Jump to content

Comma quibbling: Difference between revisions

→‎{{header|PHP}}: Don't modify the array; eliminate separate 2-item case (which the 3+-item case already handled); refactor to use switch instead of if/then
(→‎{{header|UNIX Shell}}: Add Zsh-specific version)
(→‎{{header|PHP}}: Don't modify the array; eliminate separate 2-item case (which the 3+-item case already handled); refactor to use switch instead of if/then)
Line 3,425:
<syntaxhighlight lang="php"><?php
 
function quibble($arr) {
 
$words = switch (count($arr);) {
 
case 0:
if($words == 0){
return '{}';
 
}elseif($words == 1){
case 1:
return '"{{'.$arr[0].'}'}";
}elseif($words == 2){
 
return '{'.$arr[0].' and '.$arr[1].'}';
default:
}else{
return '{'. $left = implode(', ', array_splicearray_slice($arr, 0, -1) ). ' and '.$arr[0].'}';
$right = array_slice($arr, -1)[0];
}
return '"{{'.$arr[0].'left} and '.{$arr[1].'right}}'";
 
}
 
}
Line 3,451 ⟶ 3,454:
foreach ($tests as $test) {
echo quibble($test) . PHP_EOL;
}
}?></syntaxhighlight>
{{out}}
<pre>{}
1,481

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.