Comma quibbling: Difference between revisions

m
→‎{{header|PHP}}: fix formatting.
m (→‎{{header|Wren}}: Changed to Wren S/H)
m (→‎{{header|PHP}}: fix formatting.)
 
(10 intermediate revisions by 3 users not shown)
Line 220:
<pre>A>quib
{}
{ABC}
{ABC and DEF}
{ABC, DEF, G and H}</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO RETURN quibble words:
PUT "" IN result
PUT #words IN remaining
FOR word IN words:
PUT result^word IN result
PUT remaining-1 IN remaining
IF remaining = 1: PUT result^" and " IN result
IF remaining > 1: PUT result^", " IN result
RETURN "{" ^ result ^ "}"
 
PUT {} IN tests
INSERT {} IN tests
INSERT {[1]: "ABC"} IN tests
INSERT {[1]: "ABC"; [2]: "DEF"} IN tests
INSERT {[1]: "ABC"; [2]: "DEF"; [3]: "G"; [4]: "H"} IN tests
FOR test IN tests:
WRITE quibble test/</syntaxhighlight>
{{out}}
<pre>{}
{ABC}
{ABC and DEF}
Line 1,294 ⟶ 1,318:
{ABC and DEF}
{ABC, DEF, G and H}</pre>
 
=={{header|Commodore BASIC}}==
The Commodore character set has no curly braces, so I substituted square brackets. The solution could no doubt be improved, but some of the elegance of other solutions is not possible simply because a FOR loop always executes at least once, even if its parameters would seem to indicate that it should not.
<syntaxhighlight lang="basic">100 DIM A$(3)
110 FOR TC=1 TO 4
120 : READ A: IF A=0 THEN 160
130 : FOR I=0 TO A-1
140 : READ A$(I)
150 : NEXT I
160 : GOSUB 200
170 : PRINT CQ$
180 NEXT TC
190 END
200 CQ$="["
210 IF A < 1 THEN 290
220 CQ$ = CQ$ + A$(0)
230 IF A < 2 THEN 290
240 IF A < 3 THEN 280
250 FOR I=1 TO A - 2
260 : CQ$ = CQ$ + ", " + A$(I)
270 NEXT I
280 CQ$ = CQ$ + " AND " + A$(A - 1)
290 CQ$ = CQ$ + "]"
300 RETURN
310 DATA 0
320 DATA 1, ABC
330 DATA 2, ABC, DEF
340 DATA 4, ABC, DEF, G, H
</syntaxhighlight>
{{out}}
<pre>[]
[ABC]
[ABC AND DEF]
[ABC, DEF, G AND H]</pre>
 
=={{header|Common Lisp}}==
Line 1,399 ⟶ 1,457:
{{out}}
<pre>["{}", "{ABC}", "{ABC and DEF}", "{ABC, DEF, G and H}"]</pre>
 
=={{header|dc}}==
<syntaxhighlight lang="dc">[(q)uibble: main entry point. print brackets, calling n in between if stack not
empty.]sx
[ [{]n z 0 !=n [}]pR ]sq
 
[(n)onempty: if more than 1 item, call m. then print top of stack.]sx
[ z 1 !=m n ]sn
 
[(m)ore: call f to flip stack into r register, call p to print most of it,
then pop the last item back onto the main stack so it's there to be printed
after we return]sx
[ lfx lpx Lr ]sm
 
[(f)lip: utility routine to reverse the stack into the r register]sx
[ Sr z 0 !=f ]sf
 
[(p)rint: get next item from stack in r register and print it. If there are
more than 2 items left on the register stack (which never drops below one
item), print a comma (c) and recurse. If there are exactly two items left,
print " and " (a) and return.]sx
[ Lr n 2 yr >c 2 yr =a 2 yr >p]sp
 
[(c)omma: utility routine to print a comma followed by a space]sx
[ [, ]n ]sc
 
[(a)and: utility routine to print " and "]sx
[ [ and ]n ]sa
 
[run tests]sx
lqx
[ABC] lqx
[ABC] [DEF] lqx
[ABC] [DEF] [G] [H] lqx</syntaxhighlight>
 
{{out}}
<pre>{}
{ABC}
{ABC and DEF}
{ABC, DEF, G and H}</pre>
 
=={{header|DCL}}==
Line 1,443 ⟶ 1,541:
{ABC and DEF}
{ABC, DEF, G and H}</pre>
 
=={{header|Delphi}}==
See [[#Pascal|Pascal]].
Line 3,326 ⟶ 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].'}';
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 "{{$left} and {$right}}";
 
}
 
}
Line 3,352 ⟶ 3,454:
foreach ($tests as $test) {
echo quibble($test) . PHP_EOL;
}
}</syntaxhighlight>
?></syntaxhighlight>
{{out}}
<pre>{}
Line 3,932 ⟶ 4,035:
{ABC and DEF}
{ABC, DEF, G, and H}</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Quibble>>
<Prout <Quibble ('ABC')>>
<Prout <Quibble ('ABC') ('DEF')>>
<Prout <Quibble ('ABC') ('DEF') ('G') ('H')>>;
};
 
Quibble {
e.X = '{' <Quibble1 e.X> '}';
};
 
Quibble1 {
= ;
(e.Word) = e.Word;
(e.Word1) (e.Word2) = e.Word1 ' and ' e.Word2;
(e.Word) e.Words = e.Word ', ' <Quibble1 e.Words>;
};</syntaxhighlight>
{{out}}
<pre>{}
{ABC}
{ABC and DEF}
{ABC, DEF, G and H}</pre>
 
=={{header|REXX}}==
Line 4,408 ⟶ 4,535:
(tree-bind (: last . lead) (reverse list)
`{@{(nreverse lead) ", "}@(if lead " and ")@last}`))</syntaxhighlight>
 
=={{header|Uiua}}==
{{works with|Uiua|0.10.0-dev.1}}
 
<syntaxhighlight lang="Uiua">
Tests ← {{}
{"ABC"}
{"ABC" "DEF"}
{"ABC" "DEF" "G" "H"}}
Jam ← ◇⊂◇⊂:□
Quibble ← |1 ⟨""◌|°□⊢|Jam" and "°⊟|Quibble⊂⊃(□Jam", "°⊟↙2)(↘2)⟩↧3⧻.
Wrap ← ⊂⊂"{" : "}" Quibble
⍚(&pWrap) Tests
</syntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
{{trans|AWK}}
{{works with|Korn Shell}}
<syntaxhighlight lang="bash">quibble() {
{{works with|Z Shell}}
# Here awk(1) is easier than sed(1).
<syntaxhighlight lang="bash">
awk 'BEGIN {
quibble() {
for (i = 1; i < ARGC - 2; i++) s = s ARGV[i] ", "
printf '{'
i = ARGC - 2; if (i > 0) s = s ARGV[i] " and "
i = ARGC - 1; ifwhile (i( $# > 0)2 s = s)); ARGV[i]do
printf "{'%s}\n", s' "$1"
shift
exit 0
done
}' "$@"
if (( $# )); then
}
printf '%s' "$1"
shift
fi
if (( $# )); then
printf ' and %s' "$1"
fi
printf '%s\n' '}'
}</syntaxhighlight>
 
With a slight modification, it will work in any POSIX shell, or even older Bourne-compatible shells as long as they have functions and <b>printf</b>:
 
<syntaxhighlight lang="sh">
quibble() {
printf '{'
while [ $# -gt 2 ]; do
printf '%s, ' "$1"
shift
done
if [ $# -gt 0 ]; then
printf '%s' "$1"
shift
fi
if [ $# -gt 0 ]; then
printf ' and %s' "$1"
fi
printf '%s\n' '}'
}</syntaxhighlight>
 
Going the other way, Zsh-specific code can be more compact:
 
{{works with|Z Shell}}
 
<syntaxhighlight lang="zsh">quibble() {
printf '{'
if (( $# > 1 )) printf '%s and ' ${(j:, :)@[1,-2]}
printf '%s}\n' $@[-1]
}</syntaxhighlight>
 
The test code is the same either way:
<syntaxhighlight lang="sh">
quibble
quibble ABC
1,480

edits