I didn’t want to search for it, so I made mine:
- python:
def join(l):
""" Joins pieces with a connector recursively """
if type(l) is list:
if len(l) < 3:
exit("ERROR: there aren't any pieces to join")
c0 = join(l[0])
c2 = join(l[2:]) if len(l) > 3 else join(l[2])
if all([c0,c2]):
return "%s%s%s" % (c0, l[1], c2)
else:
return c0 or c2
else:
return l
- php:
/**
* Function to construct a "clean" text by passing
* an array of words and its connectors.
*/
function join_clean($words){
if(is_array($words)){
if(count($words) < 3)
die("ERROR: there aren't any pieces to join");
$c0 = join_clean($words[0]);
if(count($words) > 3)
$c2 = join_clean(array_slice($words, 2));
else
$c2 = join_clean($words[2]);
if($c0 && $c2)
return $c0.$words[1].$c2;
elseif($c0)
return $c0;
else
return $c2;
}else
return $words;
}