@ 2024-03-29T07:37:33Z <?php
$tests = [
'' => [],
'one' => ['one'],
'one and two' => ['one', 'two'],
'one, two and three' => ['one', 'two', 'three'],
'one, two, three and four' => ['one', 'two', 'three', 'four'],
'one, two, three, four and five' => ['one', 'two', 'three', 'four', 'five'],
];
function mickmackusa1(array $words, string $separator = ', ', string $conjunction = ' and '): string {
return implode($separator, $words + ['last' => implode($conjunction, array_splice($words, -2))]);
}
function mickmackusa2(array $words, string $separator = ', ', string $conjunction = ' and ') {
return implode($conjunction, [...(array) (implode($separator, array_splice($words, 0, -1)) ?: []), ...$words]);
}
function angryDan(array $words, $separator = ', ', $conjunction = ' and '): mixed {
$last = array_pop($words);
if ($words) {
return implode($separator, $words) . $conjunction . $last;
}
return $last;
}
function deceze(array $words, string $separator = ', ', string $conjunction = ' and '): string {
return join($conjunction, array_filter(array_merge(array(join(', ', array_slice($words, 0, -1))), array_slice($words, -1)), 'strlen'));
}
function jercSi(array $words, string $separator = ', ', string $conjunction = ' and '): string {
$lastItem = array_pop($words); // c
$text = implode($separator, $words); // a, b
$text .= $conjunction . $lastItem;
return $text;
}
function enrique(array $words, string $separator = ', ', string $conjunction = ' and '): mixed {
$str = array_pop($words);
if ($words)
$str = implode($separator, $words) . $conjunction . $str;
return $str;
}
function jJWright(array $words, string $separator = ', ', string $conjunction = ' and '): string {
$last = array_pop($words);
$remaining = count($words);
return ($remaining ? implode(', ',$words) . $conjunction : '') . $last;
}
function sevavietl(array $words, string $separator = ', ', string $conjunction = ' and '): string {
return implode(
array_map(
function ($item, $glue) { return $item . $glue; },
$words,
array_slice(array_fill(0, count($words), $separator) + ['last' => $conjunction], 2)
)
);
}
function vision(array $words, string $separator = ', ', string $conjunction = ' and '): string {
$words[] = implode($conjunction, array_splice($words, -2));
return implode($separator, $words);
}
function uruapanmexicansong(array $words, string $separator = ', ', string $conjunction = ' and '): string {
$str = implode($separator, $words);
if (count($words) == 2) {
return str_replace($separator, $conjunction, $str);
}
return preg_replace("/[{$separator}](?!.*[{$separator}])/", $conjunction, $str);
}
function planetX(array $words, string $separator = ', ', string $conjunction = 'and '): string {
ob_start();
foreach ($words as $key => $value) {
if (count($words) - 1 == $key)
echo $conjunction . $value;
elseif (count($words) - 2 == $key)
echo $value . " ";
else
echo $value . $separator;
}
return ob_get_clean();
}
function alexRussell(array $words, string $separator = ', ', string $conjunction = 'and '): string {
ob_start();
foreach ($words as $k => $model) {
echo $model;
if (!preg_match("/s|S$/", $model))
echo 's';
if (isset($words[$k + 1])) {
echo $separator;
if (!isset($words[$k + 2]))
echo $conjunction;
}
}
return ob_get_clean();
}
function astha(array $words, string $separator = ', ', string $conjunction = ' and ') {
$str = "";
$lenArr = sizeof($words);
for($i = 0; $i < $lenArr; $i++) {
if ($i == 0)
$str .= $words[$i];
elseif ($i == ($lenArr-1))
$str .= $conjunction . $words[$i];
else
$str .= $separator . $words[$i];
}
return $str;
}
function zkent(array $words, string $separator = ', ', string $conjunction = 'and '): mixed {
if (count($words) == 1) {
return array_pop($words);
} elseif (count($words) == 2) {
return implode(" $conjunction", $words);
} else {
$last = array_pop($words);
$list = implode($separator, $words);
$list .= $separator . $conjunction . $last;
return $list;
}
}
function podolinek(array $words, string $separator = ', ', string $conjunction = ' and ') {
if (count($words) === 0) {
return "";
} elseif (count($words) === 1) {
return $words[0];
}
$firstItems = implode($separator, array_slice($words, 0, -1));
$lastItem = array_slice($words, -1)[0];
return $firstItems . $conjunction . $lastItem;
}
function greenButterfly(array $words, string $separator = ', ', string $conjunction = ' and '): string {
return implode($separator, array_slice($words, 0, -1)) . $conjunction . array_slice($words, -1)[0];
}
function josh(array $words, string $separator = ',', string $conjunction = ' and '): string {
if (count($words) > 1) {
return sprintf(
'%s%s %s %s',
implode($separator, array_slice($words, 0, -1)),
count($words) > 2 ? $separator : '',
$conjunction ?: '',
array_pop($words)
);
}
return implode('', $words);
}
function jackB(array $words, string $separator = ', ', string $conjunction = 'and '): string {
$words[count($words) - 1] = $conjunction . $words[count($words) - 1];
return implode($separator, $words);
}
function blazejKlisz(array $words, string $separator = ', ', string $conjunction = ' and '): string {
return implode($conjunction, array_filter(array_reverse(array_merge(array(array_pop($words)), array(implode($separator, $words))))));
}
function raviMisra(array $words, string $separator = ', ', string $conjunction = ' and '): string {
if (empty($words)) {
return '';
}
$glued = implode($separator, $words);
$pos = strrpos($glued, $separator);
if ($pos !== false) {
$glued = substr_replace($glued, $conjunction, $pos, strlen($separator));
}
return $glued;
}
printf("| %20s| 0️⃣ | 1️⃣ | 2️⃣ | 3️⃣ | 4️⃣ | 5️⃣ |\n", 'user \ elements');
$funcs = [
'mickmackusa1',
'mickmackusa2',
'deceze',
'jJWright',
'sevavietl',
'vision',
'astha',
'podolinek',
'blazejKlisz',
'raviMisra',
'angryDan',
'enrique',
'planetX',
'jercSi',
'zkent',
'uruapanmexicansong',
'josh',
'josh',
'alexRussell',
'greenButterfly',
'jackB'
];
foreach ($funcs as $func) {
echo "--------------------------------------------------------------------\n";
printf(
"| %20s| %s | %s | %s | %s | %s | %s |\n",
$func,
...array_map(
fn($result, $expected) => $result === $expected ? '✅': '💩',
array_map($func, $tests),
array_keys($tests)
)
);
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.4.13 0.012 0.011 18.34 8.4.12 0.005 0.004 24.27 8.4.11 0.012 0.008 22.57 8.4.10 0.015 0.008 19.05 8.4.9 0.009 0.008 18.77 8.4.8 0.006 0.009 20.70 8.4.7 0.010 0.009 18.64 8.4.6 0.005 0.005 19.08 8.4.5 0.012 0.009 18.92 8.4.4 0.014 0.007 18.61 8.4.3 0.009 0.013 19.11 8.4.2 0.018 0.003 20.07 8.4.1 0.009 0.000 19.98 8.3.26 0.012 0.008 17.11 8.3.25 0.011 0.010 19.01 8.3.24 0.013 0.008 17.30 8.3.23 0.010 0.004 16.89 8.3.22 0.012 0.006 19.10 8.3.21 0.005 0.004 18.61 8.3.20 0.006 0.003 17.03 8.3.19 0.004 0.004 17.36 8.3.18 0.009 0.009 17.44 8.3.17 0.013 0.007 19.04 8.3.16 0.004 0.015 17.36 8.3.15 0.013 0.006 19.31 8.3.14 0.012 0.003 19.28 8.3.13 0.005 0.005 18.82 8.3.12 0.006 0.003 19.06 8.3.11 0.006 0.003 20.94 8.3.10 0.004 0.004 24.06 8.3.9 0.012 0.006 26.77 8.3.8 0.000 0.010 17.97 8.3.7 0.015 0.006 18.68 8.3.6 0.010 0.007 16.98 8.3.5 0.024 0.000 17.04 8.3.4 0.015 0.009 20.08 8.3.3 0.018 0.006 19.52 8.3.2 0.014 0.010 22.02 8.3.1 0.012 0.009 21.85 8.3.0 0.013 0.010 19.46 8.2.29 0.008 0.011 20.71 8.2.28 0.010 0.011 18.56 8.2.27 0.000 0.015 17.52 8.2.26 0.016 0.003 17.17 8.2.25 0.004 0.004 16.93 8.2.24 0.007 0.003 19.02 8.2.23 0.006 0.003 22.58 8.2.22 0.003 0.006 37.54 8.2.21 0.004 0.004 26.77 8.2.20 0.004 0.007 17.25 8.2.19 0.010 0.013 17.13 8.2.18 0.006 0.009 17.13 8.2.17 0.020 0.000 19.33 8.2.16 0.017 0.003 20.35 8.2.15 0.015 0.004 19.22 8.2.14 0.015 0.003 20.60 8.2.13 0.011 0.007 20.48 8.2.12 0.019 0.000 19.38 8.2.11 0.012 0.006 19.57 8.2.10 0.007 0.011 19.44 8.2.9 0.010 0.010 19.33 8.2.8 0.013 0.006 19.49 8.2.7 0.006 0.015 18.95 8.2.6 0.013 0.010 19.40 8.2.5 0.012 0.009 19.52 8.2.4 0.014 0.010 19.23 8.2.3 0.013 0.013 19.15 8.2.2 0.010 0.010 19.25 8.2.1 0.012 0.009 19.06 8.2.0 0.010 0.007 19.12 8.1.33 0.012 0.006 21.99 8.1.32 0.008 0.012 16.84 8.1.31 0.006 0.012 21.93 8.1.30 0.010 0.013 20.15 8.1.29 0.004 0.008 30.84 8.1.28 0.016 0.006 25.92 8.1.27 0.003 0.014 22.26 8.1.26 0.010 0.007 22.20 8.1.25 0.006 0.010 22.32 8.1.24 0.012 0.006 18.95 8.1.23 0.010 0.007 18.84 8.1.22 0.013 0.003 19.11 8.1.21 0.010 0.007 18.69 8.1.20 0.013 0.003 18.94 8.1.19 0.016 0.003 18.88 8.1.18 0.012 0.008 18.76 8.1.17 0.004 0.015 18.94 8.1.16 0.007 0.010 18.66 8.1.15 0.010 0.007 18.66 8.1.14 0.017 0.000 18.94 8.1.13 0.010 0.010 18.80 8.1.12 0.013 0.006 18.92 8.1.11 0.009 0.009 18.81 8.1.10 0.007 0.010 20.11 8.1.9 0.003 0.013 18.88 8.1.8 0.007 0.010 19.94 8.1.7 0.012 0.004 18.96 8.1.6 0.006 0.011 18.98 8.1.5 0.010 0.007 18.70 8.1.4 0.007 0.014 18.92 8.1.3 0.015 0.003 19.01 8.1.2 0.010 0.006 20.17 8.1.1 0.007 0.010 18.78 8.1.0 0.010 0.007 18.69
preferences:dark mode live preview ace vim emacs key bindings
35.07 ms | 403 KiB | 5 Q