Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Comma-quibbling/00-META.yaml
Normal file
2
Task/Comma-quibbling/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Comma_quibbling
|
||||
22
Task/Comma-quibbling/00-TASK.txt
Normal file
22
Task/Comma-quibbling/00-TASK.txt
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
Comma quibbling is a task originally set by Eric Lippert in his [http://blogs.msdn.com/b/ericlippert/archive/2009/04/15/comma-quibbling.aspx blog].
|
||||
|
||||
|
||||
;Task:
|
||||
|
||||
Write a function to generate a string output which is the concatenation of input words from a list/sequence where:
|
||||
# An input of no words produces the output string of just the two brace characters "{}".
|
||||
# An input of just one word, e.g. ["ABC"], produces the output string of the word inside the two braces, e.g. "{ABC}".
|
||||
# An input of two words, e.g. ["ABC", "DEF"], produces the output string of the two words inside the two braces with the words separated by the string " and ", e.g. "{ABC and DEF}".
|
||||
# An input of three or more words, e.g. ["ABC", "DEF", "G", "H"], produces the output string of all but the last word separated by ", " with the last word separated by " and " and all within braces; e.g. "{ABC, DEF, G and H}".
|
||||
|
||||
<br>
|
||||
Test your function with the following series of inputs showing your output here on this page:
|
||||
* [] # (No input words).
|
||||
* ["ABC"]
|
||||
* ["ABC", "DEF"]
|
||||
* ["ABC", "DEF", "G", "H"]
|
||||
|
||||
<br>
|
||||
Note: Assume words are non-empty strings of uppercase characters for this task.
|
||||
<br><br>
|
||||
|
||||
13
Task/Comma-quibbling/11l/comma-quibbling.11l
Normal file
13
Task/Comma-quibbling/11l/comma-quibbling.11l
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
F quibble(words)
|
||||
R S words.len
|
||||
0
|
||||
‘{}’
|
||||
1
|
||||
‘{’words[0]‘}’
|
||||
E
|
||||
‘{’words[0.<(len)-1].join(‘, ’)‘ and ’words.last‘}’
|
||||
|
||||
print(quibble([‘’] * 0))
|
||||
print(quibble([‘ABC’]))
|
||||
print(quibble([‘ABC’, ‘DEF’]))
|
||||
print(quibble([‘ABC’, ‘DEF’, ‘G’, ‘H’]))
|
||||
77
Task/Comma-quibbling/360-Assembly/comma-quibbling.360
Normal file
77
Task/Comma-quibbling/360-Assembly/comma-quibbling.360
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
* Comma quibbling 13/03/2017
|
||||
COMMAQUI CSECT
|
||||
USING COMMAQUI,R13 base register
|
||||
B 72(R15) skip savearea
|
||||
DC 17F'0' savearea
|
||||
STM R14,R12,12(R13) save previous context
|
||||
ST R13,4(R15) link backward
|
||||
ST R15,8(R13) link forward
|
||||
LR R13,R15 set addressability
|
||||
LA R6,1 i=1
|
||||
DO WHILE=(C,R6,LE,=A(N)) do i=1 to hbound(t)
|
||||
LR R1,R6 i
|
||||
SLA R1,5 *32
|
||||
LA R2,T-32 @t(0)
|
||||
AR R1,R2 @t(i)
|
||||
MVC S1,0(R1) s1=t(i)
|
||||
MVC S2,=CL32'{' s2='{'
|
||||
LA R8,S2+1 s2ins=1
|
||||
MVC I2,=F'0' i2=0
|
||||
LA R7,1 j=1
|
||||
DO WHILE=(C,R7,LE,=A(L'T)) do j=1 to length(t)
|
||||
LA R1,S1 @s1
|
||||
BCTR R1,0 @s1-1
|
||||
AR R1,R7 @s1-1+j
|
||||
MVC CJ,0(R1) cj=mid(s1,j,1)
|
||||
CLI CJ,C' ' if cj=' '
|
||||
BE EXITJ then goto exitj
|
||||
IF CLI,CJ,EQ,C',' THEN if cj="," then
|
||||
MVC 0(2,R8),=C', ' s2=s2||", "
|
||||
LA R8,2(R8) s2ins=s2ins+2
|
||||
LR R0,R8 s2ins
|
||||
LA R1,S2+1 @s2+1
|
||||
SR R0,R1 len(s2)-1
|
||||
ST R0,I2 i2=len(s2)-1
|
||||
ELSE , else
|
||||
MVC 0(1,R8),CJ s2=s2||cj
|
||||
LA R8,1(R8) s2ins=s2ins+1
|
||||
ENDIF , endif
|
||||
LA R7,1(R7) j++
|
||||
ENDDO , enddo j
|
||||
EXITJ MVI 0(R8),C'}' s2=s2||"}"
|
||||
LA R8,1(R8) s2ins=s2ins+1
|
||||
L R0,I2 i2
|
||||
IF LTR,R0,NZ,R0 THEN if i2<>0 then
|
||||
MVC S2B,S2 s2b=mid(s2,1,i2-1)
|
||||
LA R1,S2B-1 @s2b-1
|
||||
A R1,I2 +i2
|
||||
MVC 0(5,R1),=C' and ' s2b||" and "
|
||||
LA R1,5(R1) +5
|
||||
LA R2,S2+1 @s2+1
|
||||
A R2,I2 +i2
|
||||
LR R3,R8 s2ins
|
||||
LA R0,S2+1 @s2+1
|
||||
SR R3,R0 s2ins-(@s2+1)
|
||||
S R3,I2 -i2
|
||||
BCTR R3,0 -1
|
||||
EX R3,XMVC s2b||=mid(s2,i2+2)
|
||||
MVC S2,S2B s2=mid(s2,1,i2-1)||" and "||mid(s2,i2+2)
|
||||
ENDIF , endif
|
||||
XPRNT S2,L'S2 print s2
|
||||
LA R6,1(R6) i++
|
||||
ENDDO , enddo i
|
||||
L R13,4(0,R13) restore previous savearea pointer
|
||||
LM R14,R12,12(R13) restore previous context
|
||||
XR R15,R15 rc=0
|
||||
BR R14 exit
|
||||
XMVC MVC 0(0,R1),0(R2) mvc @r1,@r2
|
||||
N EQU (TEND-T)/L'T items of t
|
||||
T DC CL32' ',CL32'ABC',CL32'ABC,DEF',CL32'ABC,DEF,G,H'
|
||||
TEND DS 0C
|
||||
I2 DS F
|
||||
S1 DS CL(L'T)
|
||||
S2 DS CL(L'T)
|
||||
S2B DS CL(L'T)
|
||||
CJ DS CL1
|
||||
YREGS
|
||||
END COMMAQUI
|
||||
83
Task/Comma-quibbling/8080-Assembly/comma-quibbling.8080
Normal file
83
Task/Comma-quibbling/8080-Assembly/comma-quibbling.8080
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
org 100h
|
||||
jmp demo
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Given a list of strings in HL, and a pointer in DE, write
|
||||
;; the resulting string starting at DE.
|
||||
quibble: mvi a,'{' ; Write the first {,
|
||||
stax d
|
||||
inx d ; And increment the pointer
|
||||
push h ; Keep start of list
|
||||
call strseqlen ; Get length of list
|
||||
pop h ; Restore start of list
|
||||
xra a ; Is the list empty?
|
||||
ora b
|
||||
jz quibend ; If empty list, we're done.
|
||||
quibcopy: call strcpy ; Copy current string into output
|
||||
inx h ; Advance input pointer to next string
|
||||
dcr b ; Decrement counter
|
||||
jz quibend ; If zero, that was the last string
|
||||
push h ; Push input pointer
|
||||
mov a,b ; Is the counter 1 now?
|
||||
cpi 1
|
||||
lxi h,quibcomma ; Add a comma and space,
|
||||
jnz quibsep ; unless the counter was 1,
|
||||
lxi h,quiband ; then use " and "
|
||||
quibsep: call strcpy ; Copy the separator into the output
|
||||
pop h ; Restore the input pointer
|
||||
jmp quibcopy ; Do the next string in the list
|
||||
quibend: mvi a,'}' ; Write the final '}'
|
||||
stax d
|
||||
inx d
|
||||
mvi a,'$' ; And write a string terminator
|
||||
stax d
|
||||
ret
|
||||
quibcomma: db ', $'
|
||||
quiband: db ' and $'
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Copy the string under HL to DE until the terminator $.
|
||||
;; The terminator is not copied; HL and DE are left one byte
|
||||
;; beyond the last byte copied.
|
||||
strcpy: mov a,m ; Get byte from input
|
||||
cpi '$' ; Are we at the end?
|
||||
rz ; Then stop.
|
||||
stax d ; Otherwise, store byte at output
|
||||
inx h ; Increment the pointers
|
||||
inx d
|
||||
jmp strcpy ; Copy next byte.
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Return in B the amount of strings in the string list in HL
|
||||
strseqlen: mvi a,'$' ; String end
|
||||
mvi b,0 ; String counter
|
||||
count: cmp m ; Empty string?
|
||||
rz ; Then we're done
|
||||
inr b ; Otherwise, we have a string
|
||||
strsrch: cmp m ; Find the end of the string
|
||||
inx h
|
||||
jnz strsrch
|
||||
jmp count
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Demo code: run 'quibble' on the examples
|
||||
demo: mvi c,4 ; Four examples
|
||||
lxi h,examples ; Pointer to first example
|
||||
example: push b ; Push example count
|
||||
lxi d,buffer ; Into the buffer,
|
||||
call quibble ; write the output of comma-quibbling
|
||||
inx h ; Point to next example
|
||||
push h ; Save pointer to next example
|
||||
lxi d,buffer ; Write the output to the console
|
||||
mvi c,9
|
||||
call 5
|
||||
lxi d,newline ; Write a newline to the console
|
||||
mvi c,9
|
||||
call 5
|
||||
pop h ; Restore example pointer
|
||||
pop b ; Restore example counter
|
||||
dcr c ; If not zero,
|
||||
jnz example ; do the next example.
|
||||
ret
|
||||
newline: db 10,13,'$'
|
||||
examples: db '$'
|
||||
db 'ABC$$'
|
||||
db 'ABC$DEF$$'
|
||||
db 'ABC$DEF$G$H$$'
|
||||
buffer:
|
||||
77
Task/Comma-quibbling/ALGOL-68/comma-quibbling.alg
Normal file
77
Task/Comma-quibbling/ALGOL-68/comma-quibbling.alg
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
# returns a string ( assumed to be of space-separated words ) with the words #
|
||||
# separated by ", ", except for the last which is separated from the rest by #
|
||||
# " and ". The list is enclosed by braces #
|
||||
PROC to list = ( STRING words ) STRING:
|
||||
BEGIN
|
||||
# count the number of words #
|
||||
INT word count := 0;
|
||||
BOOL in word := FALSE;
|
||||
FOR char pos FROM LWB words TO UPB words
|
||||
DO
|
||||
IF NOT is upper( words[ char pos ] )
|
||||
THEN
|
||||
# not an upper-case letter, possibly a word has been ended #
|
||||
in word := FALSE
|
||||
ELSE
|
||||
# not a delimitor, possibly the start of a word #
|
||||
IF NOT in word
|
||||
THEN
|
||||
# we are starting a new word #
|
||||
word count +:= 1;
|
||||
in word := TRUE
|
||||
FI
|
||||
FI
|
||||
OD;
|
||||
|
||||
# format the result #
|
||||
STRING result := "{";
|
||||
in word := FALSE;
|
||||
INT word number := 0;
|
||||
FOR char pos FROM LWB words TO UPB words
|
||||
DO
|
||||
IF NOT is upper( words[ char pos ] )
|
||||
THEN
|
||||
# not an upper-case letter, possibly a word has been ended #
|
||||
in word := FALSE
|
||||
ELSE
|
||||
# not a delimitor, possibly the start of a word #
|
||||
IF NOT in word
|
||||
THEN
|
||||
# we are starting a new word #
|
||||
word number +:= 1;
|
||||
in word := TRUE;
|
||||
IF word number > 1
|
||||
THEN
|
||||
# second or subsequent word - need a separator #
|
||||
result +:= IF word number = word count
|
||||
THEN # final word #
|
||||
" and "
|
||||
ELSE # non-final word #
|
||||
", "
|
||||
FI
|
||||
FI
|
||||
FI;
|
||||
# add the character to the result #
|
||||
result +:= words[ char pos ]
|
||||
FI
|
||||
OD;
|
||||
|
||||
result + "}"
|
||||
END # to list # ;
|
||||
|
||||
|
||||
# procedure to test the to list PROC #
|
||||
PROC test to list = ( STRING words ) VOID:
|
||||
print( ( ( words
|
||||
+ ": "
|
||||
+ to list( words )
|
||||
)
|
||||
, newline
|
||||
)
|
||||
);
|
||||
|
||||
# test the to list PROC #
|
||||
test to list( "" );
|
||||
test to list( "ABC" );
|
||||
test to list( "ABC DEF" );
|
||||
test to list( "ABC DEF G H" )
|
||||
107
Task/Comma-quibbling/ALGOL-W/comma-quibbling.alg
Normal file
107
Task/Comma-quibbling/ALGOL-W/comma-quibbling.alg
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
begin
|
||||
|
||||
% returns a list of the words contained in wordString, separated by ", ", %
|
||||
% except for the last which is separated from the rest by " and ". %
|
||||
% The words are enclosed by braces %
|
||||
string(256) procedure toList ( string(256) value words ) ;
|
||||
begin
|
||||
string(256) list;
|
||||
integer wordCount, wordNumber, listPos;
|
||||
logical inWord;
|
||||
|
||||
% returns true if ch is an upper-case letter, false otherwise %
|
||||
% assumes the letters are consecutive in the character set %
|
||||
% (as in ascii) would not be correct if the character set was %
|
||||
% ebcdic (as in the original implementations of Algol W) %
|
||||
logical procedure isUpper ( string(1) value ch ) ; ch >= "A" and ch <= "Z" ;
|
||||
|
||||
% adds a character to the result %
|
||||
procedure addChar( string(1) value ch ) ;
|
||||
begin
|
||||
list( listPos // 1 ) := ch;
|
||||
listPos := listPos + 1;
|
||||
end addChar ;
|
||||
|
||||
% adds a string to the result %
|
||||
procedure addString( string(256) value str
|
||||
; integer value len
|
||||
) ;
|
||||
for strPos := 0 until len - 1 do addChar( str( strPos // 1 ) );
|
||||
|
||||
% count the number of words %
|
||||
|
||||
wordCount := 0;
|
||||
inWord := false;
|
||||
for charPos := 0 until 255
|
||||
do begin
|
||||
if isUpper( words( charPos // 1 ) ) then begin
|
||||
% not an upper-case letter, possibly a word has been ended %
|
||||
inWord := false
|
||||
end
|
||||
else begin
|
||||
% not a delimitor, possibly the start of a word %
|
||||
if not inWord then begin
|
||||
% we are starting a new word %
|
||||
wordCount := wordCount + 1;
|
||||
inWord := true
|
||||
end if_not_inWord
|
||||
end
|
||||
end for_charPos;
|
||||
|
||||
% format the result %
|
||||
|
||||
list := "";
|
||||
listPos := 0;
|
||||
inWord := false;
|
||||
wordNumber := 0;
|
||||
|
||||
addChar( "{" );
|
||||
|
||||
for charPos := 0 until 255
|
||||
do begin
|
||||
if not isUpper( words( charPos // 1 ) ) then begin
|
||||
% not an upper-case letter, possibly a word has been ended %
|
||||
inWord := false
|
||||
end
|
||||
else begin
|
||||
% not a delimitor, possibly the start of a word %
|
||||
if not inWord then begin
|
||||
% we are starting a new word %
|
||||
wordNumber := wordNumber + 1;
|
||||
inWord := true;
|
||||
if wordNumber > 1 then begin
|
||||
% second or subsequent word - need a separator %
|
||||
if wordNumber = wordCount then addString( " and ", 5 ) % final word %
|
||||
else addString( ", ", 2 ) % non-final word %
|
||||
end
|
||||
end;
|
||||
% add the character to the result %
|
||||
addChar( words( charPos // 1 ) )
|
||||
end
|
||||
end for_charPos ;
|
||||
|
||||
addChar( "}" );
|
||||
|
||||
list
|
||||
end toList ;
|
||||
|
||||
|
||||
% procedure to test the toList procedure %
|
||||
procedure testToList ( string(256) value words ) ;
|
||||
begin
|
||||
string(256) list;
|
||||
list := toList( words );
|
||||
write( s_w := 0
|
||||
, words( 0 // 32 )
|
||||
, ": "
|
||||
, list( 0 // 32 )
|
||||
)
|
||||
end testToList ;
|
||||
|
||||
% test the toList procedure %
|
||||
testToList( "" );
|
||||
testToList( "ABC" );
|
||||
testToList( "ABC DEF" );
|
||||
testToList( "ABC DEF G H" );
|
||||
|
||||
end.
|
||||
1
Task/Comma-quibbling/APL/comma-quibbling.apl
Normal file
1
Task/Comma-quibbling/APL/comma-quibbling.apl
Normal file
|
|
@ -0,0 +1 @@
|
|||
quibble ← 1⌽'}{',(∊⊢,¨2↓(' and ' ''),⍨(⊂', ')⍴⍨⍴)
|
||||
13
Task/Comma-quibbling/AWK/comma-quibbling.awk
Normal file
13
Task/Comma-quibbling/AWK/comma-quibbling.awk
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
function quibble(a, n, i, s) {
|
||||
for (i = 1; i < n - 1; i++) s = s a[i] ", "
|
||||
i = n - 1; if (i > 0) s = s a[i] " and "
|
||||
if (n > 0) s = s a[n]
|
||||
return "{" s "}"
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
print quibble(a, 0)
|
||||
n = split("ABC", b); print quibble(b, n)
|
||||
n = split("ABC DEF", c); print quibble(c, n)
|
||||
n = split("ABC DEF G H", d); print quibble(d, n)
|
||||
}
|
||||
59
Task/Comma-quibbling/Action-/comma-quibbling.action
Normal file
59
Task/Comma-quibbling/Action-/comma-quibbling.action
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
DEFINE PTR="CARD"
|
||||
|
||||
PROC Append(CHAR ARRAY text,suffix)
|
||||
BYTE POINTER srcPtr,dstPtr
|
||||
BYTE len
|
||||
|
||||
len=suffix(0)
|
||||
IF text(0)+len>255 THEN
|
||||
len=255-text(0)
|
||||
FI
|
||||
IF len THEN
|
||||
srcPtr=suffix+1
|
||||
dstPtr=text+text(0)+1
|
||||
MoveBlock(dstPtr,srcPtr,len)
|
||||
text(0)==+suffix(0)
|
||||
FI
|
||||
RETURN
|
||||
|
||||
PROC Quibble(PTR ARRAY items INT count CHAR ARRAY result)
|
||||
INT i
|
||||
|
||||
result(0)=0
|
||||
Append(result,"(")
|
||||
FOR i=0 TO count-1
|
||||
DO
|
||||
Append(result,items(i))
|
||||
IF i=count-2 THEN
|
||||
Append(result," and ")
|
||||
ELSEIF i<count-2 THEN
|
||||
Append(result,", ")
|
||||
FI
|
||||
OD
|
||||
Append(result,")")
|
||||
RETURN
|
||||
|
||||
PROC Test(PTR ARRAY items BYTE count)
|
||||
CHAR ARRAY result(256)
|
||||
|
||||
Quibble(items,count,result)
|
||||
PrintE(result)
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
PTR ARRAY items(5)
|
||||
|
||||
Test(items,0)
|
||||
|
||||
items(0)="ABC"
|
||||
Test(items,1)
|
||||
|
||||
items(1)="DEF"
|
||||
Test(items,2)
|
||||
|
||||
items(2)="G"
|
||||
Test(items,3)
|
||||
|
||||
items(3)="H"
|
||||
Test(items,4)
|
||||
RETURN
|
||||
17
Task/Comma-quibbling/Ada/comma-quibbling.ada
Normal file
17
Task/Comma-quibbling/Ada/comma-quibbling.ada
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
with Ada.Text_IO, Ada.Command_Line; use Ada.Command_Line;
|
||||
|
||||
procedure Comma_Quibble is
|
||||
|
||||
begin
|
||||
case Argument_Count is
|
||||
when 0 => Ada.Text_IO.Put_Line("{}");
|
||||
when 1 => Ada.Text_IO.Put_Line("{" & Argument(1) & "}");
|
||||
when others =>
|
||||
Ada.Text_IO.Put("{");
|
||||
for I in 1 .. Argument_Count-2 loop
|
||||
Ada.Text_IO.Put(Argument(I) & ", ");
|
||||
end loop;
|
||||
Ada.Text_IO.Put(Argument(Argument_Count-1) & " and " &
|
||||
Argument(Argument_Count) & "}");
|
||||
end case;
|
||||
end Comma_Quibble;
|
||||
185
Task/Comma-quibbling/AppleScript/comma-quibbling.applescript
Normal file
185
Task/Comma-quibbling/AppleScript/comma-quibbling.applescript
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
-- quibble :: [String] -> String
|
||||
on quibble(xs)
|
||||
if length of xs > 1 then
|
||||
set applyCommas to ¬
|
||||
compose([curry(my intercalate)'s |λ|(", "), my |reverse|, my tail])
|
||||
|
||||
intercalate(" and ", ap({applyCommas, my head}, {|reverse|(xs)}))
|
||||
else
|
||||
concat(xs)
|
||||
end if
|
||||
end quibble
|
||||
|
||||
-- TEST -----------------------------------------------------------------------
|
||||
on run
|
||||
script braces
|
||||
on |λ|(x)
|
||||
"{" & x & "}"
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
unlines(map(compose({braces, quibble}), ¬
|
||||
append({{}, {"ABC"}, {"ABC", "DEF"}, {"ABC", "DEF", "G", "H"}}, ¬
|
||||
map(|words|, ¬
|
||||
{"One two three four", "Me myself I", "Jack Jill", "Loner"}))))
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS ----------------------------------------------------------
|
||||
|
||||
-- A list of functions applied to a list of arguments
|
||||
-- (<*> | ap) :: [(a -> b)] -> [a] -> [b]
|
||||
on ap(fs, xs)
|
||||
set {intFs, intXs} to {length of fs, length of xs}
|
||||
set lst to {}
|
||||
repeat with i from 1 to intFs
|
||||
tell mReturn(item i of fs)
|
||||
repeat with j from 1 to intXs
|
||||
set end of lst to |λ|(contents of (item j of xs))
|
||||
end repeat
|
||||
end tell
|
||||
end repeat
|
||||
return lst
|
||||
end ap
|
||||
|
||||
-- (++) :: [a] -> [a] -> [a]
|
||||
on append(xs, ys)
|
||||
xs & ys
|
||||
end append
|
||||
|
||||
-- compose :: [(a -> a)] -> (a -> a)
|
||||
on compose(fs)
|
||||
script
|
||||
on |λ|(x)
|
||||
script
|
||||
on |λ|(a, f)
|
||||
mReturn(f)'s |λ|(a)
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldr(result, x, fs)
|
||||
end |λ|
|
||||
end script
|
||||
end compose
|
||||
|
||||
-- concat :: [[a]] -> [a] | [String] -> String
|
||||
on concat(xs)
|
||||
script append
|
||||
on |λ|(a, b)
|
||||
a & b
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
if length of xs > 0 and class of (item 1 of xs) is string then
|
||||
set unit to ""
|
||||
else
|
||||
set unit to {}
|
||||
end if
|
||||
foldl(append, unit, xs)
|
||||
end concat
|
||||
|
||||
-- curry :: (Script|Handler) -> Script
|
||||
on curry(f)
|
||||
script
|
||||
on |λ|(a)
|
||||
script
|
||||
on |λ|(b)
|
||||
|λ|(a, b) of mReturn(f)
|
||||
end |λ|
|
||||
end script
|
||||
end |λ|
|
||||
end script
|
||||
end curry
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
-- foldr :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldr(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from lng to 1 by -1
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldr
|
||||
|
||||
-- head :: [a] -> a
|
||||
on head(xs)
|
||||
if length of xs > 0 then
|
||||
item 1 of xs
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end head
|
||||
|
||||
-- intercalate :: Text -> [Text] -> Text
|
||||
on intercalate(strText, lstText)
|
||||
set {dlm, my text item delimiters} to {my text item delimiters, strText}
|
||||
set strJoined to lstText as text
|
||||
set my text item delimiters to dlm
|
||||
return strJoined
|
||||
end intercalate
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- |reverse| :: [a] -> [a]
|
||||
on |reverse|(xs)
|
||||
if class of xs is text then
|
||||
(reverse of characters of xs) as text
|
||||
else
|
||||
reverse of xs
|
||||
end if
|
||||
end |reverse|
|
||||
|
||||
-- tail :: [a] -> [a]
|
||||
on tail(xs)
|
||||
if length of xs > 1 then
|
||||
items 2 thru -1 of xs
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end tail
|
||||
|
||||
-- unlines :: [String] -> String
|
||||
on unlines(xs)
|
||||
intercalate(linefeed, xs)
|
||||
end unlines
|
||||
|
||||
-- words :: String -> [String]
|
||||
on |words|(s)
|
||||
words of s
|
||||
end |words|
|
||||
21
Task/Comma-quibbling/Arturo/comma-quibbling.arturo
Normal file
21
Task/Comma-quibbling/Arturo/comma-quibbling.arturo
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
quibble: $[sequence :block][
|
||||
if? 0 = size sequence
|
||||
-> return "{}"
|
||||
|
||||
if? 1 = size sequence
|
||||
-> return ~"{|sequence\0|}"
|
||||
|
||||
last: pop 'sequence
|
||||
return ~« {|join.with: ", " sequence| and |last|}
|
||||
]
|
||||
|
||||
sentences: [
|
||||
[]
|
||||
["ABC"]
|
||||
["ABC" "DEF"]
|
||||
["ABC" "DEF" "G" "H"]
|
||||
]
|
||||
|
||||
loop sentences 'sentence [
|
||||
print quibble sentence
|
||||
]
|
||||
13
Task/Comma-quibbling/Astro/comma-quibbling.astro
Normal file
13
Task/Comma-quibbling/Astro/comma-quibbling.astro
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
fun quibble(s):
|
||||
let result = s.join(' and ').replace(|| and ||, ", ", length(s) - 1)
|
||||
return "{ $result }"
|
||||
|
||||
let s = [
|
||||
[]
|
||||
["ABC"]
|
||||
["ABC", "DEF"]
|
||||
["ABC", "DEF", "G", "H"]
|
||||
]
|
||||
|
||||
for i in s:
|
||||
print(quibble i)
|
||||
18
Task/Comma-quibbling/AutoHotkey/comma-quibbling.ahk
Normal file
18
Task/Comma-quibbling/AutoHotkey/comma-quibbling.ahk
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
MsgBox % quibble([])
|
||||
MsgBox % quibble(["ABC"])
|
||||
MsgBox % quibble(["ABC", "DEF"])
|
||||
MsgBox % quibble(["ABC", "DEF", "G", "H"])
|
||||
|
||||
quibble(d) {
|
||||
s:=""
|
||||
for i, e in d
|
||||
{
|
||||
if (i<d.MaxIndex()-1)
|
||||
s:= s . e . ", "
|
||||
else if (i=d.MaxIndex()-1)
|
||||
s:= s . e . " and "
|
||||
else
|
||||
s:= s . e
|
||||
}
|
||||
return "{" . s . "}"
|
||||
}
|
||||
47
Task/Comma-quibbling/BCPL/comma-quibbling.bcpl
Normal file
47
Task/Comma-quibbling/BCPL/comma-quibbling.bcpl
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
get "libhdr"
|
||||
|
||||
// Add a character to the end of a string
|
||||
let addch(s, ch) be
|
||||
$( s%0 := s%0 + 1
|
||||
s%(s%0) := ch
|
||||
$)
|
||||
// Add s2 to the end of s1
|
||||
and adds(s1, s2) be
|
||||
for i = 1 to s2%0 do
|
||||
addch(s1, s2%i)
|
||||
|
||||
// Comma quibbling on strs, which should be a 0-terminated
|
||||
// vector of string pointers.
|
||||
let quibble(strs, buf) = valof
|
||||
$( buf%0 := 0
|
||||
addch(buf, '{')
|
||||
until !strs = 0 do
|
||||
$( addch(buf, '"')
|
||||
adds(buf, !strs)
|
||||
addch(buf, '"')
|
||||
unless strs!1 = 0
|
||||
test strs!2 = 0
|
||||
then adds(buf, " and ")
|
||||
else adds(buf, ", ")
|
||||
strs := strs + 1
|
||||
$)
|
||||
addch(buf, '}')
|
||||
resultis buf
|
||||
$)
|
||||
|
||||
let start() be
|
||||
$( let words = vec 4
|
||||
let buf = vec 63
|
||||
|
||||
words!0 := 0
|
||||
writef("%S*N", quibble(words, buf))
|
||||
|
||||
words!0 := "ABC" ; words!1 := 0
|
||||
writef("%S*N", quibble(words, buf))
|
||||
|
||||
words!1 := "DEF" ; words!2 := 0
|
||||
writef("%S*N", quibble(words, buf))
|
||||
|
||||
words!2 := "G" ; words!3 := "H" ; words!4 := 0
|
||||
writef("%S*N", quibble(words, buf))
|
||||
$)
|
||||
41
Task/Comma-quibbling/Batch-File/comma-quibbling.bat
Normal file
41
Task/Comma-quibbling/Batch-File/comma-quibbling.bat
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
::THE MAIN THING...
|
||||
echo.
|
||||
set inp=[]
|
||||
call :quibble
|
||||
set inp=["ABC"]
|
||||
call :quibble
|
||||
set inp=["ABC","DEF"]
|
||||
call :quibble
|
||||
set inp=["ABC","DEF","G","H"]
|
||||
call :quibble
|
||||
echo.
|
||||
pause
|
||||
exit /b
|
||||
::/THE MAIN THING...
|
||||
|
||||
::THE FUNCTION
|
||||
:quibble
|
||||
set cont=0
|
||||
set proc=%inp:[=%
|
||||
set proc=%proc:]=%
|
||||
|
||||
for %%x in (%proc%) do (
|
||||
set /a cont+=1
|
||||
set x=%%x
|
||||
set str!cont!=!x:"=!
|
||||
)
|
||||
set /a bef=%cont%-1
|
||||
set output=%str1%
|
||||
if %cont%==2 (set output=%str1% and %str2%)
|
||||
if %cont% gtr 2 (
|
||||
for /l %%y in (2,1,%bef%) do (
|
||||
set output=!output!^, !str%%y!
|
||||
)
|
||||
set output=!output! and !str%cont%!
|
||||
)
|
||||
echo {!output!}
|
||||
goto :EOF
|
||||
::/THE FUNCTION
|
||||
16
Task/Comma-quibbling/Bracmat/comma-quibbling.bracmat
Normal file
16
Task/Comma-quibbling/Bracmat/comma-quibbling.bracmat
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
( :?L1
|
||||
& ABC:?L2
|
||||
& ABC DEF:?L3
|
||||
& ABC DEF G H:?L4
|
||||
& L1 L2 L3 L4:?names
|
||||
& ( quibble
|
||||
= w
|
||||
. !arg:%?w (% %:?arg)
|
||||
& !w ", " quibble$!arg
|
||||
| !arg:%?w %?arg&!w " and " quibble$!arg
|
||||
| !arg
|
||||
)
|
||||
& (concat=.str$("{" quibble$!arg "}"))
|
||||
& whl
|
||||
' (!names:%?name ?names&out$(!name concat$!!name))
|
||||
);
|
||||
26
Task/Comma-quibbling/C++/comma-quibbling.cpp
Normal file
26
Task/Comma-quibbling/C++/comma-quibbling.cpp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#include <iostream>
|
||||
|
||||
template<class T>
|
||||
void quibble(std::ostream& o, T i, T e) {
|
||||
o << "{";
|
||||
if (e != i) {
|
||||
T n = i++;
|
||||
const char* more = "";
|
||||
while (e != i) {
|
||||
o << more << *n;
|
||||
more = ", ";
|
||||
n = i++;
|
||||
}
|
||||
o << (*more?" and ":"") << *n;
|
||||
}
|
||||
o << "}";
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
char const* a[] = {"ABC","DEF","G","H"};
|
||||
for (int i=0; i<5; i++) {
|
||||
quibble(std::cout, a, a+i);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
33
Task/Comma-quibbling/C-sharp/comma-quibbling.cs
Normal file
33
Task/Comma-quibbling/C-sharp/comma-quibbling.cs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace CommaQuibbling
|
||||
{
|
||||
internal static class Program
|
||||
{
|
||||
#region Static Members
|
||||
private static string Quibble(string[] input)
|
||||
{
|
||||
return
|
||||
String.Format("{{{0}}}",
|
||||
String.Join("",
|
||||
input.Reverse().Zip(
|
||||
new [] { "", " and " }.Concat(Enumerable.Repeat(", ", int.MaxValue)),
|
||||
(x, y) => x + y).Reverse()));
|
||||
}
|
||||
|
||||
|
||||
private static void Main()
|
||||
{
|
||||
Console.WriteLine( Quibble( new string[] {} ) );
|
||||
Console.WriteLine( Quibble( new[] {"ABC"} ) );
|
||||
Console.WriteLine( Quibble( new[] {"ABC", "DEF"} ) );
|
||||
Console.WriteLine( Quibble( new[] {"ABC", "DEF", "G", "H"} ) );
|
||||
|
||||
Console.WriteLine( "< Press Any Key >" );
|
||||
Console.ReadKey();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
53
Task/Comma-quibbling/C/comma-quibbling.c
Normal file
53
Task/Comma-quibbling/C/comma-quibbling.c
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char *quib(const char **strs, size_t size)
|
||||
{
|
||||
|
||||
size_t len = 3 + ((size > 1) ? (2 * size + 1) : 0);
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < size; i++)
|
||||
len += strlen(strs[i]);
|
||||
|
||||
char *s = malloc(len * sizeof(*s));
|
||||
if (!s)
|
||||
{
|
||||
perror("Can't allocate memory!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
strcpy(s, "{");
|
||||
switch (size) {
|
||||
case 0: break;
|
||||
case 1: strcat(s, strs[0]);
|
||||
break;
|
||||
default: for (i = 0; i < size - 1; i++)
|
||||
{
|
||||
strcat(s, strs[i]);
|
||||
if (i < size - 2)
|
||||
strcat(s, ", ");
|
||||
else
|
||||
strcat(s, " and ");
|
||||
}
|
||||
strcat(s, strs[i]);
|
||||
break;
|
||||
}
|
||||
strcat(s, "}");
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const char *test[] = {"ABC", "DEF", "G", "H"};
|
||||
char *s;
|
||||
|
||||
for (size_t i = 0; i < 5; i++)
|
||||
{
|
||||
s = quib(test, i);
|
||||
printf("%s\n", s);
|
||||
free(s);
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
30
Task/Comma-quibbling/CLU/comma-quibbling.clu
Normal file
30
Task/Comma-quibbling/CLU/comma-quibbling.clu
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
quibble = proc (words: array[string]) returns (string)
|
||||
out: string := "{"
|
||||
last: int := array[string]$high(words)
|
||||
|
||||
for i: int in array[string]$indexes(words) do
|
||||
out := out || words[i]
|
||||
if i < last-1 then
|
||||
out := out || ", "
|
||||
elseif i = last-1 then
|
||||
out := out || " and "
|
||||
end
|
||||
end
|
||||
return(out || "}")
|
||||
end quibble
|
||||
|
||||
start_up = proc ()
|
||||
as = array[string]
|
||||
aas = array[as]
|
||||
po: stream := stream$primary_output()
|
||||
|
||||
testcases: aas := aas$
|
||||
[as$[],
|
||||
as$["ABC"],
|
||||
as$["ABC","DEF"],
|
||||
as$["ABC","DEF","G","H"]]
|
||||
|
||||
for testcase: as in aas$elements(testcases) do
|
||||
stream$putl(po, quibble(testcase))
|
||||
end
|
||||
end start_up
|
||||
75
Task/Comma-quibbling/COBOL/comma-quibbling.cobol
Normal file
75
Task/Comma-quibbling/COBOL/comma-quibbling.cobol
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
>>SOURCE FORMAT IS FREE
|
||||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. comma-quibbling-test.
|
||||
|
||||
ENVIRONMENT DIVISION.
|
||||
CONFIGURATION SECTION.
|
||||
REPOSITORY.
|
||||
FUNCTION comma-quibbling
|
||||
.
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 strs-area.
|
||||
03 strs-len PIC 9.
|
||||
03 strs PIC X(5)
|
||||
OCCURS 0 TO 9 TIMES
|
||||
DEPENDING ON strs-len.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
MOVE "ABC" TO strs (1)
|
||||
MOVE "DEF" TO strs (2)
|
||||
MOVE "G" TO strs (3)
|
||||
MOVE "H" TO strs (4)
|
||||
|
||||
PERFORM VARYING strs-len FROM 0 BY 1 UNTIL strs-len > 4
|
||||
DISPLAY FUNCTION comma-quibbling(strs-area)
|
||||
END-PERFORM
|
||||
.
|
||||
END PROGRAM comma-quibbling-test.
|
||||
|
||||
|
||||
IDENTIFICATION DIVISION.
|
||||
FUNCTION-ID. comma-quibbling.
|
||||
|
||||
DATA DIVISION.
|
||||
LOCAL-STORAGE SECTION.
|
||||
01 i PIC 9.
|
||||
|
||||
01 num-extra-words PIC 9.
|
||||
|
||||
LINKAGE SECTION.
|
||||
01 strs-area.
|
||||
03 strs-len PIC 9.
|
||||
03 strs PIC X(5)
|
||||
OCCURS 0 TO 9 TIMES
|
||||
DEPENDING ON strs-len.
|
||||
|
||||
01 str PIC X(50).
|
||||
|
||||
PROCEDURE DIVISION USING strs-area RETURNING str.
|
||||
EVALUATE strs-len
|
||||
WHEN ZERO
|
||||
MOVE "{}" TO str
|
||||
GOBACK
|
||||
|
||||
WHEN 1
|
||||
MOVE FUNCTION CONCATENATE("{", FUNCTION TRIM(strs (1)), "}")
|
||||
TO str
|
||||
GOBACK
|
||||
END-EVALUATE
|
||||
|
||||
MOVE FUNCTION CONCATENATE(FUNCTION TRIM(strs (strs-len - 1)),
|
||||
" and ", FUNCTION TRIM(strs (strs-len)), "}")
|
||||
TO str
|
||||
|
||||
IF strs-len > 2
|
||||
SUBTRACT 2 FROM strs-len GIVING num-extra-words
|
||||
PERFORM VARYING i FROM num-extra-words BY -1 UNTIL i = 0
|
||||
MOVE FUNCTION CONCATENATE(FUNCTION TRIM(strs (i)), ", ", str)
|
||||
TO str
|
||||
END-PERFORM
|
||||
END-IF
|
||||
|
||||
MOVE FUNCTION CONCATENATE("{", str) TO str
|
||||
.
|
||||
END FUNCTION comma-quibbling.
|
||||
18
Task/Comma-quibbling/Clojure/comma-quibbling.clj
Normal file
18
Task/Comma-quibbling/Clojure/comma-quibbling.clj
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
(defn quibble [sq]
|
||||
(let [sep (if (pos? (count sq)) " and " "")]
|
||||
(apply str
|
||||
(concat "{" (interpose ", " (butlast sq)) [sep (last sq)] "}"))))
|
||||
|
||||
; Or, using clojure.pprint's cl-format, which implements common lisp's format:
|
||||
(defn quibble-f [& args]
|
||||
(clojure.pprint/cl-format nil "{~{~a~#[~; and ~:;, ~]~}}" args))
|
||||
|
||||
(def test
|
||||
#(doseq [sq [[]
|
||||
["ABC"]
|
||||
["ABC", "DEF"]
|
||||
["ABC", "DEF", "G", "H"]]]
|
||||
((comp println %) sq)))
|
||||
|
||||
(test quibble)
|
||||
(test quibble-f)
|
||||
9
Task/Comma-quibbling/CoffeeScript/comma-quibbling.coffee
Normal file
9
Task/Comma-quibbling/CoffeeScript/comma-quibbling.coffee
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
quibble = ([most..., last]) ->
|
||||
'{' +
|
||||
(most.join ', ') +
|
||||
(if most.length then ' and ' else '') +
|
||||
(last or '') +
|
||||
'}'
|
||||
|
||||
console.log quibble(s) for s in [ [], ["ABC"], ["ABC", "DEF"],
|
||||
["ABC", "DEF", "G", "H" ] ]
|
||||
7
Task/Comma-quibbling/Common-Lisp/comma-quibbling.lisp
Normal file
7
Task/Comma-quibbling/Common-Lisp/comma-quibbling.lisp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(defun quibble (&rest args)
|
||||
(format t "{~{~a~#[~; and ~:;, ~]~}}" args))
|
||||
|
||||
(quibble)
|
||||
(quibble "ABC")
|
||||
(quibble "ABC" "DEF")
|
||||
(quibble "ABC" "DEF" "G" "H")
|
||||
41
Task/Comma-quibbling/Cowgol/comma-quibbling.cowgol
Normal file
41
Task/Comma-quibbling/Cowgol/comma-quibbling.cowgol
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
include "cowgol.coh";
|
||||
|
||||
sub quibble(words: [[uint8]],
|
||||
length: intptr,
|
||||
buf: [uint8]):
|
||||
(out: [uint8]) is
|
||||
sub append(s: [uint8]) is
|
||||
while [s] != 0 loop
|
||||
[buf] := [s];
|
||||
buf := @next buf;
|
||||
s := @next s;
|
||||
end loop;
|
||||
end sub;
|
||||
|
||||
out := buf;
|
||||
|
||||
append("{");
|
||||
while length > 0 loop
|
||||
append([words]);
|
||||
words := @next words;
|
||||
case length is
|
||||
when 1: break;
|
||||
when 2: append(" and ");
|
||||
when else: append(", ");
|
||||
end case;
|
||||
length := length - 1;
|
||||
end loop;
|
||||
append("}");
|
||||
|
||||
[buf] := 0;
|
||||
end sub;
|
||||
|
||||
var w1: [uint8][] := {};
|
||||
var w2: [uint8][] := {"ABC"};
|
||||
var w3: [uint8][] := {"ABC","DEF"};
|
||||
var w4: [uint8][] := {"ABC","DEF","G","H"};
|
||||
|
||||
print(quibble(&w1[0], @sizeof w1, LOMEM)); print_nl();
|
||||
print(quibble(&w2[0], @sizeof w2, LOMEM)); print_nl();
|
||||
print(quibble(&w3[0], @sizeof w3, LOMEM)); print_nl();
|
||||
print(quibble(&w4[0], @sizeof w4, LOMEM)); print_nl();
|
||||
17
Task/Comma-quibbling/D/comma-quibbling-1.d
Normal file
17
Task/Comma-quibbling/D/comma-quibbling-1.d
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import std.stdio, std.string;
|
||||
|
||||
string quibbler(in string[] seq) pure /*nothrow*/ {
|
||||
if (seq.length <= 1)
|
||||
return format("{%-(%s, %)}", seq);
|
||||
else
|
||||
return format("{%-(%s, %) and %s}", seq[0 .. $-1], seq[$-1]);
|
||||
}
|
||||
|
||||
void main() {
|
||||
//foreach (immutable test; [[],
|
||||
foreach (const test; [[],
|
||||
["ABC"],
|
||||
["ABC", "DEF"],
|
||||
["ABC", "DEF", "G", "H"]])
|
||||
test.quibbler.writeln;
|
||||
}
|
||||
10
Task/Comma-quibbling/D/comma-quibbling-2.d
Normal file
10
Task/Comma-quibbling/D/comma-quibbling-2.d
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import std.stdio, std.string, std.algorithm, std.conv, std.array;
|
||||
|
||||
enum quibbler = (in string[] a) pure =>
|
||||
"{%-(%s and %)}".format(a.length < 2 ? a :
|
||||
[a[0 .. $-1].join(", "), a.back]);
|
||||
|
||||
void main() {
|
||||
[[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]]
|
||||
.map!quibbler.writeln;
|
||||
}
|
||||
36
Task/Comma-quibbling/DCL/comma-quibbling.dcl
Normal file
36
Task/Comma-quibbling/DCL/comma-quibbling.dcl
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
$ list = "[]"
|
||||
$ gosub comma_quibbling
|
||||
$ write sys$output return_string
|
||||
$
|
||||
$ list = "[""ABC""]"
|
||||
$ gosub comma_quibbling
|
||||
$ write sys$output return_string
|
||||
$
|
||||
$ list = "[""ABC"", ""DEF""]"
|
||||
$ gosub comma_quibbling
|
||||
$ write sys$output return_string
|
||||
$
|
||||
$ list = "[""ABC"", ""DEF"", ""G"", ""H""]"
|
||||
$ gosub comma_quibbling
|
||||
$ write sys$output return_string
|
||||
$
|
||||
$ exit
|
||||
$
|
||||
$ comma_quibbling:
|
||||
$ list = list - "[" - "]"
|
||||
$ return_string = "{}"
|
||||
$ if list .eqs. "" then $ return
|
||||
$ return_string = "{" + f$element( 0, ",", list ) - """" - """"
|
||||
$ if f$locate( ",", list ) .eq. f$length( list ) then $ goto done2
|
||||
$ i = 1
|
||||
$ loop:
|
||||
$ word = f$element( i, ",", list ) - """" - """"
|
||||
$ if word .eqs. "," then $ goto done1
|
||||
$ return_string = return_string - "^" + "^," + word
|
||||
$ i = i + 1
|
||||
$ goto loop
|
||||
$ done1:
|
||||
$ return_string = f$element( 0, "^", return_string ) + " and" + ( f$element( 1, "^", return_string ) - "," )
|
||||
$ done2:
|
||||
$ return_string = return_string + "}"
|
||||
$ return
|
||||
20
Task/Comma-quibbling/EchoLisp/comma-quibbling.l
Normal file
20
Task/Comma-quibbling/EchoLisp/comma-quibbling.l
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
(lib 'match)
|
||||
|
||||
(define (quibble words)
|
||||
(match words
|
||||
[ null "{}"]
|
||||
[ (a) (format "{ %a }" a)]
|
||||
[ (a b) (format "{ %a and %a }" a b)]
|
||||
[( a ... b c) (format "{ %a %a and %a }" (for/string ([w a]) (string-append w ", ")) b c)]
|
||||
[else 'bad-input]))
|
||||
|
||||
|
||||
;; output
|
||||
|
||||
(for ([t '(() ("ABC") ("ABC" "DEF") ("ABC" "DEF" "G" "H"))])
|
||||
(writeln t '----> (quibble t)))
|
||||
|
||||
null ----> "{}"
|
||||
("ABC") ----> "{ ABC }"
|
||||
("ABC" "DEF") ----> "{ ABC and DEF }"
|
||||
("ABC" "DEF" "G" "H") ----> "{ ABC, DEF, G and H }"
|
||||
47
Task/Comma-quibbling/Eiffel/comma-quibbling.e
Normal file
47
Task/Comma-quibbling/Eiffel/comma-quibbling.e
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
class
|
||||
APPLICATION
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature
|
||||
|
||||
make
|
||||
-- Test of the feature comma_quibbling.
|
||||
local
|
||||
l: LINKED_LIST [STRING]
|
||||
do
|
||||
create l.make
|
||||
io.put_string (comma_quibbling (l) + "%N")
|
||||
l.extend ("ABC")
|
||||
io.put_string (comma_quibbling (l) + "%N")
|
||||
l.extend ("DEF")
|
||||
io.put_string (comma_quibbling (l) + "%N")
|
||||
l.extend ("G")
|
||||
l.extend ("H")
|
||||
io.put_string (comma_quibbling (l) + "%N")
|
||||
end
|
||||
|
||||
comma_quibbling (l: LINKED_LIST [STRING]): STRING
|
||||
-- Elements of 'l' seperated by a comma or an and where appropriate.
|
||||
require
|
||||
l_not_void: l /= Void
|
||||
do
|
||||
create Result.make_empty
|
||||
Result.extend ('{')
|
||||
if l.is_empty then
|
||||
Result.append ("}")
|
||||
elseif l.count = 1 then
|
||||
Result.append (l [1] + "}")
|
||||
else
|
||||
Result.append (l [1])
|
||||
across
|
||||
2 |..| (l.count - 1) as c
|
||||
loop
|
||||
Result.append (", " + l [c.item])
|
||||
end
|
||||
Result.append (" and " + l [l.count] + "}")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
16
Task/Comma-quibbling/Elixir/comma-quibbling.elixir
Normal file
16
Task/Comma-quibbling/Elixir/comma-quibbling.elixir
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
defmodule RC do
|
||||
def generate( list ), do: "{#{ generate_content(list) }}"
|
||||
|
||||
defp generate_content( [] ), do: ""
|
||||
defp generate_content( [x] ), do: x
|
||||
defp generate_content( [x1, x2] ), do: "#{x1} and #{x2}"
|
||||
defp generate_content( xs ) do
|
||||
[last, second_to_last | t] = Enum.reverse( xs )
|
||||
with_commas = for x <- t, do: x <> ","
|
||||
Enum.join(Enum.reverse([last, "and", second_to_last | with_commas]), " ")
|
||||
end
|
||||
end
|
||||
|
||||
Enum.each([[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]], fn list ->
|
||||
IO.inspect RC.generate(list)
|
||||
end)
|
||||
17
Task/Comma-quibbling/Erlang/comma-quibbling.erl
Normal file
17
Task/Comma-quibbling/Erlang/comma-quibbling.erl
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
-module( comma_quibbling ).
|
||||
|
||||
-export( [task/0] ).
|
||||
|
||||
task() -> [generate(X) || X <- [[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]]].
|
||||
|
||||
|
||||
|
||||
generate( List ) -> "{" ++ generate_content(List) ++ "}".
|
||||
|
||||
generate_content( [] ) -> "";
|
||||
generate_content( [X] ) -> X;
|
||||
generate_content( [X1, X2] ) -> string:join( [X1, "and", X2], " " );
|
||||
generate_content( Xs ) ->
|
||||
[Last, Second_to_last | T] = lists:reverse( Xs ),
|
||||
With_commas = [X ++ "," || X <- T],
|
||||
string:join(lists:reverse([Last, "and", Second_to_last | With_commas]), " ").
|
||||
14
Task/Comma-quibbling/F-Sharp/comma-quibbling-1.fs
Normal file
14
Task/Comma-quibbling/F-Sharp/comma-quibbling-1.fs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
let quibble list =
|
||||
let rec inner = function
|
||||
| [] -> ""
|
||||
| [x] -> x
|
||||
| [x;y] -> sprintf "%s and %s" x y
|
||||
| h::t -> sprintf "%s, %s" h (inner t)
|
||||
sprintf "{%s}" (inner list)
|
||||
|
||||
// test interactively
|
||||
quibble []
|
||||
quibble ["ABC"]
|
||||
quibble ["ABC"; "DEF"]
|
||||
quibble ["ABC"; "DEF"; "G"]
|
||||
quibble ["ABC"; "DEF"; "G"; "H"]
|
||||
10
Task/Comma-quibbling/F-Sharp/comma-quibbling-2.fs
Normal file
10
Task/Comma-quibbling/F-Sharp/comma-quibbling-2.fs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
> quibble [];;
|
||||
val it : string = "{}"
|
||||
> quibble ["ABC"];;
|
||||
val it : string = "{ABC}"
|
||||
> quibble ["ABC"; "DEF"];;
|
||||
val it : string = "{ABC and DEF}"
|
||||
> quibble ["ABC"; "DEF"; "G"];;
|
||||
val it : string = "{ABC, DEF and G}"
|
||||
> quibble ["ABC"; "DEF"; "G"; "H"];;
|
||||
val it : string = "{ABC, DEF, G and H}"
|
||||
1
Task/Comma-quibbling/F-Sharp/comma-quibbling-3.fs
Normal file
1
Task/Comma-quibbling/F-Sharp/comma-quibbling-3.fs
Normal file
|
|
@ -0,0 +1 @@
|
|||
let quibble quibbler quibblee = Seq.zip quibblee quibbler //Sorry, just too good a line to miss, back in my Latin classes
|
||||
6
Task/Comma-quibbling/F-Sharp/comma-quibbling-4.fs
Normal file
6
Task/Comma-quibbling/F-Sharp/comma-quibbling-4.fs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
let fN n = quibble (List.mapi(fun n _->match n with 0->"" |1-> " and " |_->", ") n |> List.rev) n
|
||||
printf "{"; fN ["ABC"; "DEF"; "G"; "H"] |> Seq.iter(fun(n,g)->printf "%s%s" n g); printfn"}"
|
||||
printf "{"; fN ["ABC"; "DEF"; "G"] |> Seq.iter(fun(n,g)->printf "%s%s" n g); printfn"}"
|
||||
printf "{"; fN ["ABC"; "DEF"] |> Seq.iter(fun(n,g)->printf "%s%s" n g); printfn"}"
|
||||
printf "{"; fN ["ABC"] |> Seq.iter(fun(n,g)->printf "%s%s" n g); printfn"}"
|
||||
printf "{"; fN [] |> Seq.iter(fun(n,g)->printf "%s%s" n g); printfn"}"
|
||||
14
Task/Comma-quibbling/Factor/comma-quibbling.factor
Normal file
14
Task/Comma-quibbling/Factor/comma-quibbling.factor
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
USING: inverse qw sequences ;
|
||||
|
||||
: (quibble) ( seq -- seq' )
|
||||
{
|
||||
{ [ { } ] [ "" ] }
|
||||
{ [ 1array ] [ ] }
|
||||
{ [ 2array ] [ " and " glue ] }
|
||||
[ unclip swap (quibble) ", " glue ]
|
||||
} switch ;
|
||||
|
||||
: quibble ( seq -- str ) (quibble) "{%s}" sprintf ;
|
||||
|
||||
{ } qw{ ABC } qw{ ABC DEF } qw{ ABC DEF G H }
|
||||
[ quibble print ] 4 napply
|
||||
16
Task/Comma-quibbling/Forth/comma-quibbling.fth
Normal file
16
Task/Comma-quibbling/Forth/comma-quibbling.fth
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
: read bl parse ;
|
||||
: not-empty? ( c-addr u -- c-addr u true | false ) ?dup-if true else drop false then ;
|
||||
: third-to-last 2rot ;
|
||||
: second-to-last 2swap ;
|
||||
|
||||
: quibble
|
||||
." {"
|
||||
read read begin read not-empty? while third-to-last type ." , " repeat
|
||||
second-to-last not-empty? if type then
|
||||
not-empty? if ." and " type then
|
||||
." }" cr ;
|
||||
|
||||
quibble
|
||||
quibble ABC
|
||||
quibble ABC DEF
|
||||
quibble ABC DEF G H
|
||||
66
Task/Comma-quibbling/Fortran/comma-quibbling.f
Normal file
66
Task/Comma-quibbling/Fortran/comma-quibbling.f
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
SUBROUTINE QUIBBLE(TEXT,OXFORDIAN) !Punctuates a list with commas and stuff.
|
||||
CHARACTER*(*) TEXT !The text, delimited by spaces.
|
||||
LOGICAL OXFORDIAN !Just so.
|
||||
INTEGER IST(6),LST(6) !Start and stop positions.
|
||||
INTEGER N,L,I !Counters.
|
||||
INTEGER L1,L2 !Fingers for the scan.
|
||||
INTEGER MSG !Output unit.
|
||||
COMMON /IODEV/MSG !Share.
|
||||
Chop the text into words.
|
||||
N = 0 !No words found.
|
||||
L = LEN(TEXT) !Multiple trailing spaces - no worries.
|
||||
L2 = 0 !Syncopation: where the previous chomp ended.
|
||||
10 L1 = L2 !Thus, where a fresh scan should follow.
|
||||
11 L1 = L1 + 1 !Advance one.
|
||||
IF (L1.GT.L) GO TO 20 !Finished yet?
|
||||
IF (TEXT(L1:L1).LE." ") GO TO 11 !No. Skip leading spaces.
|
||||
L2 = L1 !Righto, L1 is the first non-blank.
|
||||
12 L2 = L2 + 1 !Scan through the non-blanks.
|
||||
IF (L2.GT.L) GO TO 13 !Is it safe to look?
|
||||
IF (TEXT(L2:L2).GT." ") GO TO 12 !Yes. Speed through non-blanks.
|
||||
13 N = N + 1 !Righto, a word is found in TEXT(L1:L2 - 1)
|
||||
IST(N) = L1 !So, recall its first character.
|
||||
LST(N) = L2 - 1 !And its last.
|
||||
IF (L2.LT.L) GO TO 10 !Perhaps more text follows.
|
||||
Comma time...
|
||||
20 WRITE (MSG,21) "{" !Start the output.
|
||||
21 FORMAT (A,$) !The $, obviously, specifies that the line is not finished.
|
||||
DO I = 1,N !Step through the texts, there possibly being none.
|
||||
IF (I.GT.1) THEN !If there has been a predecessor, supply separators.
|
||||
IF (I.LT.N) THEN !Up to the last two, it's easy.
|
||||
WRITE (MSG,21) ", " !Always just a comma.
|
||||
ELSE IF (OXFORDIAN) THEN !But after the penultimate item, what?
|
||||
WRITE (MSG,21) ", and " !Supply the comma omitted above: a double-power separator.
|
||||
ELSE !One fewer comma, with possible ambiguity arising.
|
||||
WRITE (MSG,21) " and " !A single separator.
|
||||
END IF !So much for the style.
|
||||
END IF !Enough with the separation.
|
||||
WRITE (MSG,21) TEXT(IST(I):LST(I)) !The text at last!
|
||||
END DO !On to the next text.
|
||||
WRITE (MSG,"('}')") !End the line, marking the end of the text.
|
||||
END !That was fun.
|
||||
|
||||
PROGRAM ENCOMMA !Punctuate a list with commas.
|
||||
CHARACTER*(666) TEXT !Holds the text. Easily long enough.
|
||||
INTEGER KBD,MSG,INF !Now for some messing.
|
||||
COMMON /IODEV/MSG,KBD !Pass the word.
|
||||
KBD = 5 !Standard input.
|
||||
MSG = 6 !Standard output.
|
||||
INF = 10 !Suitable for a disc file.
|
||||
OPEN (INF,FILE="List.txt",ACTION = "READ") !Attach one.
|
||||
|
||||
10 WRITE (MSG,11) "To insert commas into lists..." !Announce.
|
||||
11 FORMAT (A) !Just the text.
|
||||
12 READ (INF,11,END = 20) TEXT !Grab the text, with trailing spaces to fill out TEXT.
|
||||
CALL QUIBBLE(TEXT,.FALSE.) !One way to quibble.
|
||||
GO TO 12 !Try for another.
|
||||
|
||||
20 REWIND (INF) !Back to the start of the file.
|
||||
WRITE (MSG,11) !Set off a bit.
|
||||
WRITE (MSG,11) "Oxford style..." !Announce the proper style.
|
||||
21 READ (INF,11,END = 30) TEXT !Grab the text.
|
||||
CALL QUIBBLE(TEXT,.TRUE.) !The other way to quibble.
|
||||
GO TO 21 !Have another try.
|
||||
|
||||
Closedown
|
||||
30 END !All files are closed by exiting.
|
||||
58
Task/Comma-quibbling/FreeBASIC/comma-quibbling.basic
Normal file
58
Task/Comma-quibbling/FreeBASIC/comma-quibbling.basic
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Sub Split(s As String, sep As String, result() As String)
|
||||
Dim As Integer i, j, count = 0
|
||||
Dim temp As String
|
||||
Dim As Integer position(Len(s) + 1)
|
||||
position(0) = 0
|
||||
For i = 0 To Len(s) - 1
|
||||
For j = 0 To Len(sep) - 1
|
||||
If s[i] = sep[j] Then
|
||||
count += 1
|
||||
position(count) = i + 1
|
||||
End If
|
||||
Next j
|
||||
Next i
|
||||
position(count + 1) = Len(s) + 1
|
||||
Redim result(count)
|
||||
For i = 1 To count + 1
|
||||
result(i - 1) = Mid(s, position(i - 1) + 1, position(i) - position(i - 1) - 1)
|
||||
Next
|
||||
End Sub
|
||||
|
||||
Function CommaQuibble(s As String) As String
|
||||
Dim i As Integer
|
||||
Dim As String result
|
||||
Dim As String words()
|
||||
s = Trim(s, Any "[]""")
|
||||
' Now remove internal quotes
|
||||
Split s, """", words()
|
||||
s = ""
|
||||
For i = 0 To UBound(words)
|
||||
s &= words(i)
|
||||
Next
|
||||
' Now split 's' using the comma as separator
|
||||
Erase words
|
||||
Split s, ",", words()
|
||||
' And re-assemble the string in the desired format
|
||||
result = "{"
|
||||
For i = 0 To UBound(words)
|
||||
If i = 0 Then
|
||||
result &= words(i)
|
||||
ElseIf i = UBound(words) Then
|
||||
result &= " and " & words(i)
|
||||
Else
|
||||
result &= ", " + words(i)
|
||||
EndIf
|
||||
Next
|
||||
Return result & "}"
|
||||
End Function
|
||||
|
||||
' As 3 of the strings contain embedded quotes these need to be doubled in FB
|
||||
Print CommaQuibble("[]")
|
||||
Print CommaQuibble("[""ABC""]")
|
||||
Print CommaQuibble("[""ABC"",""DEF""]")
|
||||
Print CommaQuibble("[""ABC"",""DEF"",""G"",""H""]")
|
||||
Print
|
||||
Print "Press any key to quit the program"
|
||||
Sleep
|
||||
13
Task/Comma-quibbling/Frink/comma-quibbling.frink
Normal file
13
Task/Comma-quibbling/Frink/comma-quibbling.frink
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
quibble[enum] :=
|
||||
{
|
||||
list = toArray[enum] // This makes it work on any enumerating expression
|
||||
size = length[list]
|
||||
if size >= 2
|
||||
return "{" + join[", ", first[list, size-1]] + " and " + last[list] + "}"
|
||||
else
|
||||
return "{" + join["", list] + "}"
|
||||
}
|
||||
|
||||
data = [[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]]
|
||||
for line = data
|
||||
println[quibble[line]]
|
||||
37
Task/Comma-quibbling/FutureBasic/comma-quibbling.basic
Normal file
37
Task/Comma-quibbling/FutureBasic/comma-quibbling.basic
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
local fn CommaQuibber( string as CFStringRef ) as CFStringRef
|
||||
CFStringRef tempStr
|
||||
NSUInteger i
|
||||
|
||||
tempStr = fn StringByReplacingOccurrencesOfString( string, @"[", @"" )
|
||||
tempStr = fn StringByReplacingOccurrencesOfString( tempStr, @"]", @"" )
|
||||
tempStr = fn StringByReplacingOccurrencesOfString( tempStr, @" ", @"" )
|
||||
tempStr = fn StringByReplacingOccurrencesOfString( tempStr, @"\"", @"" )
|
||||
|
||||
CFMutableStringRef quibStr = fn MutableStringWithCapacity(0)
|
||||
|
||||
CFArrayRef arr = fn StringComponentsSeparatedByString( tempStr, @"," )
|
||||
NSUInteger count = len(arr)
|
||||
select switch ( count )
|
||||
case 0 : MutableStringSetString( quibStr, @"{}" ) : break
|
||||
case 1 : MutableStringSetString( quibStr, fn StringWithFormat( @"{%@}", arr[0] ) ) : break
|
||||
case 2 : MutableStringSetString( quibStr, fn StringWithFormat( @"{%@ and %@}", arr[0], arr[1] ) ) : break
|
||||
case else
|
||||
MutableStringAppendFormat( quibStr, @"{" )
|
||||
for i = 0 to count -1
|
||||
if ( i != count -1 )
|
||||
MutableStringAppendFormat( quibStr, @"%@, ", arr[i] )
|
||||
else
|
||||
MutableStringAppendFormat( quibStr, @"and %@}", arr[i] )
|
||||
end if
|
||||
next
|
||||
end select
|
||||
end fn = quibStr
|
||||
|
||||
NSLog( @"%@", fn CommaQuibber( @"[]" ) )
|
||||
NSLog( @"%@", fn CommaQuibber( @"[\"ABC\"]" ) )
|
||||
NSLog( @"%@", fn CommaQuibber( @"[\"ABC\", \"DEF\"]" ) )
|
||||
NSLog( @"%@", fn CommaQuibber( @"[\"ABC\", \"DEF\", \"G\", \"H\"]" ) )
|
||||
|
||||
HandleEvents
|
||||
15
Task/Comma-quibbling/Gambas/comma-quibbling.gambas
Normal file
15
Task/Comma-quibbling/Gambas/comma-quibbling.gambas
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
Public Sub Main()
|
||||
Dim sInput As String[] = ["", "ABC", "ABC DEF", "ABC DEF G H"]
|
||||
Dim sTemp As String
|
||||
|
||||
For Each sTemp In sInput
|
||||
Print sTemp & " = ";
|
||||
sTemp = Replace(sTemp, " ", ",")
|
||||
If RInStr(sTemp, ",") > 0 Then
|
||||
sTemp = Mid(sTemp, 1, RInStr(sTemp, ",") - 1) & " and " & Mid(sTemp, RInStr(sTemp, ",") + 1)
|
||||
End If
|
||||
sTemp = "{" & sTemp & "}"
|
||||
Print sTemp
|
||||
Next
|
||||
|
||||
End
|
||||
30
Task/Comma-quibbling/Go/comma-quibbling.go
Normal file
30
Task/Comma-quibbling/Go/comma-quibbling.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func q(s []string) string {
|
||||
switch len(s) {
|
||||
case 0:
|
||||
return "{}"
|
||||
case 1:
|
||||
return "{" + s[0] + "}"
|
||||
case 2:
|
||||
return "{" + s[0] + " and " + s[1] + "}"
|
||||
default:
|
||||
return "{" +
|
||||
strings.Join(s[:len(s)-1], ", ") +
|
||||
" and " +
|
||||
s[len(s)-1] +
|
||||
"}"
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(q([]string{}))
|
||||
fmt.Println(q([]string{"ABC"}))
|
||||
fmt.Println(q([]string{"ABC", "DEF"}))
|
||||
fmt.Println(q([]string{"ABC", "DEF", "G", "H"}))
|
||||
}
|
||||
1
Task/Comma-quibbling/Groovy/comma-quibbling-1.groovy
Normal file
1
Task/Comma-quibbling/Groovy/comma-quibbling-1.groovy
Normal file
|
|
@ -0,0 +1 @@
|
|||
def commaQuibbling = { it.size() < 2 ? "{${it.join(', ')}}" : "{${it[0..-2].join(', ')} and ${it[-1]}}" }
|
||||
4
Task/Comma-quibbling/Groovy/comma-quibbling-2.groovy
Normal file
4
Task/Comma-quibbling/Groovy/comma-quibbling-2.groovy
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
['{}': [], '{ABC}': ['ABC'], '{ABC and DEF}': ['ABC', 'DEF'], '{ABC, DEF, G and H}': ['ABC', 'DEF', 'G', 'H']].each { expected, input ->
|
||||
println "Verifying commaQuibbling($input) == $expected"
|
||||
assert commaQuibbling(input) == expected
|
||||
}
|
||||
9
Task/Comma-quibbling/Haskell/comma-quibbling-1.hs
Normal file
9
Task/Comma-quibbling/Haskell/comma-quibbling-1.hs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
quibble ws = "{" ++ quibbles ws ++ "}"
|
||||
where quibbles [] = ""
|
||||
quibbles [a] = a
|
||||
quibbles [a,b] = a ++ " and " ++ b
|
||||
quibbles (a:bs) = a ++ ", " ++ quibbles bs
|
||||
|
||||
main = mapM_ (putStrLn . quibble) $
|
||||
[[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]] ++
|
||||
(map words ["One two three four", "Me myself I", "Jack Jill", "Loner" ])
|
||||
15
Task/Comma-quibbling/Haskell/comma-quibbling-2.hs
Normal file
15
Task/Comma-quibbling/Haskell/comma-quibbling-2.hs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import Data.List (intercalate)
|
||||
|
||||
quibble :: [String] -> String
|
||||
quibble ws
|
||||
| length ws > 1 =
|
||||
intercalate
|
||||
" and "
|
||||
([intercalate ", " . reverse . tail, head] <*> [reverse ws])
|
||||
| otherwise = concat ws
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_ (putStrLn . (`intercalate` ["{", "}"]) . quibble) $
|
||||
[[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]] ++
|
||||
(words <$> ["One two three four", "Me myself I", "Jack Jill", "Loner"])
|
||||
9
Task/Comma-quibbling/Icon/comma-quibbling.icon
Normal file
9
Task/Comma-quibbling/Icon/comma-quibbling.icon
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
procedure main()
|
||||
every write(quibble([] | ["ABC"] | ["ABC","DEF"] | ["ABC","DEF","G","H"]))
|
||||
end
|
||||
|
||||
procedure quibble(A)
|
||||
join := s := ""
|
||||
while s := pull(A)||join||s do join := if *join = 0 then " and " else ", "
|
||||
return "{"||s||"}"
|
||||
end
|
||||
3
Task/Comma-quibbling/J/comma-quibbling-1.j
Normal file
3
Task/Comma-quibbling/J/comma-quibbling-1.j
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
quibLast2=: ' and ' joinstring (2 -@<. #) {. ]
|
||||
withoutLast2=: ([: # _2&}.) {. ]
|
||||
quibble=: '{', '}' ,~ ', ' joinstring withoutLast2 , <@quibLast2
|
||||
6
Task/Comma-quibbling/J/comma-quibbling-2.j
Normal file
6
Task/Comma-quibbling/J/comma-quibbling-2.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
Tests=: (<<<3){(i.5)<@{."0 1;:'ABC DEF G H'
|
||||
quibble every Tests
|
||||
{}
|
||||
{ABC}
|
||||
{ABC and DEF}
|
||||
{ABC, DEF, G and H}
|
||||
2
Task/Comma-quibbling/J/comma-quibbling-3.j
Normal file
2
Task/Comma-quibbling/J/comma-quibbling-3.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
commaand=: 1 ;@}.&, ] ,.~ 1 |.!.(<' and ') (<', ')"0
|
||||
quibble=: '{','}',~ commaand
|
||||
21
Task/Comma-quibbling/Java/comma-quibbling.java
Normal file
21
Task/Comma-quibbling/Java/comma-quibbling.java
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
public class Quibbler {
|
||||
|
||||
public static String quibble(String[] words) {
|
||||
String qText = "{";
|
||||
for(int wIndex = 0; wIndex < words.length; wIndex++) {
|
||||
qText += words[wIndex] + (wIndex == words.length-1 ? "" :
|
||||
wIndex == words.length-2 ? " and " :
|
||||
", ";
|
||||
}
|
||||
qText += "}";
|
||||
return qText;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(quibble(new String[]{}));
|
||||
System.out.println(quibble(new String[]{"ABC"}));
|
||||
System.out.println(quibble(new String[]{"ABC", "DEF"}));
|
||||
System.out.println(quibble(new String[]{"ABC", "DEF", "G"}));
|
||||
System.out.println(quibble(new String[]{"ABC", "DEF", "G", "H"}));
|
||||
}
|
||||
}
|
||||
13
Task/Comma-quibbling/JavaScript/comma-quibbling-1.js
Normal file
13
Task/Comma-quibbling/JavaScript/comma-quibbling-1.js
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
function quibble(words) {
|
||||
return "{" +
|
||||
words.slice(0, words.length-1).join(",") +
|
||||
(words.length > 1 ? " and " : "") +
|
||||
(words[words.length-1] || '') +
|
||||
"}";
|
||||
}
|
||||
|
||||
[[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]].forEach(
|
||||
function(s) {
|
||||
console.log(quibble(s));
|
||||
}
|
||||
);
|
||||
141
Task/Comma-quibbling/JavaScript/comma-quibbling-2.js
Normal file
141
Task/Comma-quibbling/JavaScript/comma-quibbling-2.js
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// ----------------- COMMA QUIBBLING -----------------
|
||||
|
||||
// quibble :: [String] -> String
|
||||
const quibble = xs =>
|
||||
1 < xs.length ? (
|
||||
intercalate(' and ')(
|
||||
ap([
|
||||
compose(
|
||||
intercalate(', '),
|
||||
reverse,
|
||||
tail
|
||||
),
|
||||
head
|
||||
])([reverse(xs)])
|
||||
)
|
||||
) : concat(xs);
|
||||
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
const main = () =>
|
||||
unlines(
|
||||
map(compose(x => '{' + x + '}', quibble))(
|
||||
append([
|
||||
[],
|
||||
["ABC"],
|
||||
["ABC", "DEF"],
|
||||
["ABC", "DEF", "G", "H"]
|
||||
])(
|
||||
map(words)([
|
||||
"One two three four",
|
||||
"Me myself I",
|
||||
"Jack Jill",
|
||||
"Loner"
|
||||
])
|
||||
)
|
||||
));
|
||||
|
||||
|
||||
// ---------------- GENERIC FUNCTIONS ----------------
|
||||
|
||||
// ap (<*>) :: [(a -> b)] -> [a] -> [b]
|
||||
const ap = fs =>
|
||||
// The sequential application of each of a list
|
||||
// of functions to each of a list of values.
|
||||
// apList([x => 2 * x, x => 20 + x])([1, 2, 3])
|
||||
// -> [2, 4, 6, 21, 22, 23]
|
||||
xs => fs.flatMap(f => xs.map(f));
|
||||
|
||||
|
||||
// append (++) :: [a] -> [a] -> [a]
|
||||
const append = xs =>
|
||||
// A list defined by the
|
||||
// concatenation of two others.
|
||||
ys => xs.concat(ys);
|
||||
|
||||
|
||||
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
const compose = (...fs) =>
|
||||
// A function defined by the right-to-left
|
||||
// composition of all the functions in fs.
|
||||
fs.reduce(
|
||||
(f, g) => x => f(g(x)),
|
||||
x => x
|
||||
);
|
||||
|
||||
|
||||
// concat :: [[a]] -> [a]
|
||||
// concat :: [String] -> String
|
||||
const concat = xs => (
|
||||
ys => 0 < ys.length ? (
|
||||
ys.every(Array.isArray) ? (
|
||||
[]
|
||||
) : ''
|
||||
).concat(...ys) : ys
|
||||
)(xs);
|
||||
|
||||
|
||||
// head :: [a] -> a
|
||||
const head = xs => (
|
||||
ys => ys.length ? (
|
||||
ys[0]
|
||||
) : undefined
|
||||
)(list(xs));
|
||||
|
||||
|
||||
// intercalate :: String -> [String] -> String
|
||||
const intercalate = s =>
|
||||
// The concatenation of xs
|
||||
// interspersed with copies of s.
|
||||
xs => xs.join(s);
|
||||
|
||||
|
||||
// list :: StringOrArrayLike b => b -> [a]
|
||||
const list = xs =>
|
||||
// xs itself, if it is an Array,
|
||||
// or an Array derived from xs.
|
||||
Array.isArray(xs) ? (
|
||||
xs
|
||||
) : Array.from(xs || []);
|
||||
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = f =>
|
||||
// The list obtained by applying f
|
||||
// to each element of xs.
|
||||
// (The image of xs under f).
|
||||
xs => [...xs].map(f);
|
||||
|
||||
|
||||
// reverse :: [a] -> [a]
|
||||
const reverse = xs =>
|
||||
'string' !== typeof xs ? (
|
||||
xs.slice(0).reverse()
|
||||
) : xs.split('').reverse().join('');
|
||||
|
||||
|
||||
// tail :: [a] -> [a]
|
||||
const tail = xs =>
|
||||
// A new list consisting of all
|
||||
// items of xs except the first.
|
||||
xs.slice(1);
|
||||
|
||||
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs =>
|
||||
// A single string formed by the intercalation
|
||||
// of a list of strings with the newline character.
|
||||
xs.join('\n');
|
||||
|
||||
|
||||
// words :: String -> [String]
|
||||
const words = s =>
|
||||
// List of space-delimited sub-strings.
|
||||
s.split(/\s+/);
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
9
Task/Comma-quibbling/JavaScript/comma-quibbling-3.js
Normal file
9
Task/Comma-quibbling/JavaScript/comma-quibbling-3.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function quibble(words) {
|
||||
var words2 = words.join()
|
||||
|
||||
var words3 = [...words2].reverse().join('');
|
||||
var res = words3.replace(",", " dna ");
|
||||
var words4 = [...res].reverse().join('');
|
||||
|
||||
return '{'+words4+'}';
|
||||
}
|
||||
6
Task/Comma-quibbling/Jq/comma-quibbling-1.jq
Normal file
6
Task/Comma-quibbling/Jq/comma-quibbling-1.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def quibble:
|
||||
if length == 0 then ""
|
||||
elif length == 1 then .[0]
|
||||
else (.[0:length-1] | join(", ")) + " and " + .[length-1]
|
||||
end
|
||||
| "{" + . + "}";
|
||||
1
Task/Comma-quibbling/Jq/comma-quibbling-2.jq
Normal file
1
Task/Comma-quibbling/Jq/comma-quibbling-2.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
( [], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]) | quibble
|
||||
5
Task/Comma-quibbling/Jq/comma-quibbling-3.jq
Normal file
5
Task/Comma-quibbling/Jq/comma-quibbling-3.jq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
jq -n -r -f Comma_quibbling.jq
|
||||
{}
|
||||
{ABC}
|
||||
{ABC and DEF}
|
||||
{ABC, DEF, G and H}
|
||||
10
Task/Comma-quibbling/Julia/comma-quibbling.julia
Normal file
10
Task/Comma-quibbling/Julia/comma-quibbling.julia
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
function quibble(arr::Array)
|
||||
if isempty(arr) rst = "" else rst = "$(arr[end])" end
|
||||
if length(arr) > 1 rst = join(arr[1:end-1], ", ") * " and " * rst end
|
||||
return "{" * rst * "}"
|
||||
end
|
||||
|
||||
@show quibble([])
|
||||
@show quibble(["ABC"])
|
||||
@show quibble(["ABC", "DEF"])
|
||||
@show quibble(["ABC", "DEF", "G", "H"])
|
||||
26
Task/Comma-quibbling/Kotlin/comma-quibbling.kotlin
Normal file
26
Task/Comma-quibbling/Kotlin/comma-quibbling.kotlin
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
// version 1.0.6
|
||||
|
||||
fun commaQuibble(s: String): String {
|
||||
val t = s.trim('[', ']').replace(" ", "").replace("\"", "")
|
||||
val words = t.split(',')
|
||||
val sb = StringBuilder("{")
|
||||
for (i in 0 until words.size) {
|
||||
sb.append(when (i) {
|
||||
0 -> ""
|
||||
words.lastIndex -> " and "
|
||||
else -> ", "
|
||||
})
|
||||
sb.append(words[i])
|
||||
}
|
||||
return sb.append("}").toString()
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val inputs = arrayOf(
|
||||
"""[]""",
|
||||
"""["ABC"]""",
|
||||
"""["ABC", "DEF"]""",
|
||||
"""["ABC", "DEF", "G", "H"]"""
|
||||
)
|
||||
for (input in inputs) println("${input.padEnd(24)} -> ${commaQuibble(input)}")
|
||||
}
|
||||
19
Task/Comma-quibbling/Lang/comma-quibbling.lang
Normal file
19
Task/Comma-quibbling/Lang/comma-quibbling.lang
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
fp.quibble = (&words) -> {
|
||||
$len $= @&words
|
||||
|
||||
$output = \{\e
|
||||
|
||||
$i
|
||||
repeat($[i], $len) {
|
||||
$output += &words[$i] ||| ($i == -|$len?\e:($i == $len - 2?\sand\s:\,\s))
|
||||
}
|
||||
|
||||
$output += \}\e
|
||||
|
||||
return $output
|
||||
}
|
||||
|
||||
fn.println(fp.quibble(fn.arrayOf()))
|
||||
fn.println(fp.quibble(fn.arrayOf(ABC)))
|
||||
fn.println(fp.quibble(fn.arrayOf(ABC, DEF)))
|
||||
fn.println(fp.quibble(fn.arrayOf(ABC, DEF, G, H)))
|
||||
23
Task/Comma-quibbling/Lasso/comma-quibbling.lasso
Normal file
23
Task/Comma-quibbling/Lasso/comma-quibbling.lasso
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#!/usr/bin/lasso9
|
||||
|
||||
local(collection =
|
||||
array(
|
||||
array,
|
||||
array("ABC"),
|
||||
array("ABC", "DEF"),
|
||||
array("ABC", "DEF", "G", "H")
|
||||
)
|
||||
)
|
||||
|
||||
with words in #collection do {
|
||||
if(#words -> size > 1) => {
|
||||
local(last = #words -> last)
|
||||
#words -> removelast
|
||||
stdoutnl('{' + #words -> join(', ') + ' and ' + #last'}')
|
||||
else(#words -> size == 1)
|
||||
stdoutnl('{' + #words -> first + '}')
|
||||
else
|
||||
stdoutnl('{}')
|
||||
}
|
||||
|
||||
}
|
||||
44
Task/Comma-quibbling/Liberty-BASIC/comma-quibbling.basic
Normal file
44
Task/Comma-quibbling/Liberty-BASIC/comma-quibbling.basic
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
do
|
||||
read in$
|
||||
if in$ ="END" then wait
|
||||
w =wordCount( in$)
|
||||
select case w
|
||||
case 0
|
||||
o$ ="{}"
|
||||
case 1
|
||||
o$ ="{" +in$ +"}"
|
||||
case 2
|
||||
o$ ="{" +word$( in$, 1) +" and " +word$( in$, 2) +"}"
|
||||
case else
|
||||
o$ ="{"
|
||||
o$ =o$ +word$( in$, 1)
|
||||
for k =2 to w -1
|
||||
o$ =o$ +", " +word$( in$, k)
|
||||
next k
|
||||
o$ =o$ +" and " +word$( in$, w) +"}"
|
||||
end select
|
||||
if w =1 then
|
||||
print "'"; in$; "'"; " held "; w; " word. "; tab( 30); o$
|
||||
else
|
||||
print "'"; in$; "'"; " held "; w; " words. "; tab( 30); o$
|
||||
end if
|
||||
loop until 0
|
||||
|
||||
wait
|
||||
|
||||
function wordCount( IN$)
|
||||
wordCount =1
|
||||
for i =1 to len( IN$)
|
||||
if mid$( IN$, i, 1) =" " then wordCount =wordCount +1
|
||||
next i
|
||||
end function
|
||||
|
||||
end
|
||||
|
||||
data "" 'No input words.
|
||||
data "ABC" 'One input word.
|
||||
data "ABC DEF" 'Two words.
|
||||
data "ABC DEF G" 'Three words.
|
||||
data "ABC DEF G H" 'Four words.
|
||||
|
||||
data "END" 'Sentinel for EOD.
|
||||
26
Task/Comma-quibbling/Logo/comma-quibbling.logo
Normal file
26
Task/Comma-quibbling/Logo/comma-quibbling.logo
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
to join :delimiter :list [:result []]
|
||||
output cond [
|
||||
[ [empty? :list] :result ]
|
||||
[ [empty? :result] (join :delimiter butfirst :list first :list) ]
|
||||
[ else (join :delimiter butfirst :list
|
||||
(word :result :delimiter first :list)) ]
|
||||
]
|
||||
end
|
||||
|
||||
to quibble :list
|
||||
local "length
|
||||
make "length count :list
|
||||
make "text (
|
||||
ifelse [:length <= 2] [
|
||||
(join "\ and\ :list)
|
||||
] [
|
||||
(join "\ and\ (sentence join ",\ butlast :list last :list))
|
||||
])
|
||||
output ifelse [empty? :text] "\{\} [(word "\{ :text "\})]
|
||||
end
|
||||
|
||||
foreach [ [] [ABC] [ABC DEF] [ABC DEF G H] ] [
|
||||
print quibble ?
|
||||
]
|
||||
|
||||
bye
|
||||
22
Task/Comma-quibbling/Lua/comma-quibbling.lua
Normal file
22
Task/Comma-quibbling/Lua/comma-quibbling.lua
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
function quibble (strTab)
|
||||
local outString, join = "{"
|
||||
for strNum = 1, #strTab do
|
||||
if strNum == #strTab then
|
||||
join = ""
|
||||
elseif strNum == #strTab - 1 then
|
||||
join = " and "
|
||||
else
|
||||
join = ", "
|
||||
end
|
||||
outString = outString .. strTab[strNum] .. join
|
||||
end
|
||||
return outString .. '}'
|
||||
end
|
||||
|
||||
local testCases = {
|
||||
{},
|
||||
{"ABC"},
|
||||
{"ABC", "DEF"},
|
||||
{"ABC", "DEF", "G", "H"}
|
||||
}
|
||||
for _, input in pairs(testCases) do print(quibble(input)) end
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
Module Checkit {
|
||||
function f$ {
|
||||
what$=mid$(trim$(letter$),2)
|
||||
what$=Left$(what$, len(what$)-1)
|
||||
flush ' erase any argument from stack
|
||||
Data param$(what$)
|
||||
m=stack.size
|
||||
document resp$="{"
|
||||
if m>2 then {
|
||||
shift m-1, 2 ' get last two as first two
|
||||
push letter$+" and "+letter$
|
||||
m-- ' one less
|
||||
shiftback m ' move to last position
|
||||
}
|
||||
while not empty {
|
||||
resp$=letter$+if$(not empty->", ", "")
|
||||
}
|
||||
=resp$+"}"
|
||||
|
||||
}
|
||||
\\ we use ? for Print
|
||||
? f$({[]})
|
||||
? f$({["ABC"]})
|
||||
? f$({["ABC", "DEF"]})
|
||||
? f$({["ABC","DEF", "G", "H"]})
|
||||
}
|
||||
Checkit
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
Module Checkit {
|
||||
function f$ {
|
||||
what$=filter$(trim$(letter$), chr$(34))
|
||||
what$=Mid$(what$, 2, len(what$)-2)
|
||||
count=Len(what$)-Len(filter$(what$,","))
|
||||
if count>2 then m=rinstr(what$, ", ") : insert m, 2 what$=" and "
|
||||
="{"+what$+"}"
|
||||
}
|
||||
? f$({[]})
|
||||
? f$({["ABC"]})
|
||||
? f$({["ABC", "DEF"]})
|
||||
? f$({["ABC","DEF", "G", "H"]})
|
||||
}
|
||||
Checkit
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
Module Checkit {
|
||||
function f$(ar) {
|
||||
flush
|
||||
Data ! ar
|
||||
m=stack.size
|
||||
document resp$="{"
|
||||
if m>2 then {
|
||||
shift m-1, 2 ' get last two as first two
|
||||
push letter$+" and "+letter$
|
||||
m-- ' one less
|
||||
shiftback m ' move to last position
|
||||
}
|
||||
while not empty {
|
||||
resp$=letter$+if$(not empty->", ", "")
|
||||
}
|
||||
=resp$+"}"
|
||||
}
|
||||
? f$((,))
|
||||
? f$(("ABC",))
|
||||
? f$(("ABC", "DEF"))
|
||||
? f$(("ABC","DEF", "G", "H"))
|
||||
}
|
||||
Checkit
|
||||
12
Task/Comma-quibbling/MATLAB/comma-quibbling.m
Normal file
12
Task/Comma-quibbling/MATLAB/comma-quibbling.m
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
function r = comma_quibbling(varargin)
|
||||
if isempty(varargin)
|
||||
r = '';
|
||||
elseif length(varargin)==1;
|
||||
r = varargin{1};
|
||||
else
|
||||
r = [varargin{end-1},' and ', varargin{end}];
|
||||
for k=length(varargin)-2:-1:1,
|
||||
r = [varargin{k}, ', ', r];
|
||||
end
|
||||
end
|
||||
end;
|
||||
23
Task/Comma-quibbling/MAXScript/comma-quibbling-1.max
Normal file
23
Task/Comma-quibbling/MAXScript/comma-quibbling-1.max
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
fn separate words: =
|
||||
(
|
||||
if words == unsupplied or words == undefined or classof words != array then return "{}"
|
||||
else
|
||||
(
|
||||
local toReturn = "{"
|
||||
local pos = 1
|
||||
while pos <= words.count do
|
||||
(
|
||||
if pos == 1 then (append toReturn words[pos]; pos+=1)
|
||||
else
|
||||
(
|
||||
if pos <= words.count-1 then (append toReturn (", "+words[pos]); pos+=1)
|
||||
else
|
||||
(
|
||||
append toReturn (" and " + words[pos])
|
||||
pos +=1
|
||||
)
|
||||
)
|
||||
)
|
||||
return (toReturn+"}")
|
||||
)
|
||||
)
|
||||
8
Task/Comma-quibbling/MAXScript/comma-quibbling-2.max
Normal file
8
Task/Comma-quibbling/MAXScript/comma-quibbling-2.max
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
separate words:#()
|
||||
"{}"
|
||||
separate words:#("ABC")
|
||||
"{ABC}"
|
||||
separate words:#("ABC","DEF")
|
||||
"{ABC and DEF}"
|
||||
separate words:#("ABC","DEF","G","H")
|
||||
"{ABC, DEF, G and H}"
|
||||
12
Task/Comma-quibbling/Maple/comma-quibbling-1.maple
Normal file
12
Task/Comma-quibbling/Maple/comma-quibbling-1.maple
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
Quibble := proc( los )
|
||||
uses StringTools;
|
||||
Fence( proc()
|
||||
if los = [] then
|
||||
""
|
||||
elif numelems( los ) = 1 then
|
||||
los[ 1 ]
|
||||
else
|
||||
cat( Join( los[ 1 .. -2 ], ", " ), " and ", los[ -1 ] )
|
||||
end if
|
||||
end(), "{", "}" )
|
||||
end proc:
|
||||
11
Task/Comma-quibbling/Maple/comma-quibbling-2.maple
Normal file
11
Task/Comma-quibbling/Maple/comma-quibbling-2.maple
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
> Quibble([]);
|
||||
"{}"
|
||||
|
||||
> Quibble( [ "ABC" ] );
|
||||
"{ABC}"
|
||||
|
||||
> Quibble( [ "ABC", "DEF" ] );
|
||||
"{ABC and DEF}"
|
||||
|
||||
> Quibble( ["ABC", "DEF", "G", "H"] );
|
||||
"{ABC, DEF, G and H}"
|
||||
4
Task/Comma-quibbling/Mathematica/comma-quibbling.math
Normal file
4
Task/Comma-quibbling/Mathematica/comma-quibbling.math
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
quibble[words___] :=
|
||||
ToString@{StringJoin@@
|
||||
Replace[Riffle[{words}, ", "],
|
||||
{most__, ", ", last_} -> {most, " and ", last}]}
|
||||
14
Task/Comma-quibbling/Miranda/comma-quibbling.miranda
Normal file
14
Task/Comma-quibbling/Miranda/comma-quibbling.miranda
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
main :: [sys_message]
|
||||
main = [Stdout (show test ++ ": {" ++ quibble test ++ "}\n") | test <- tests]
|
||||
|
||||
tests :: [[[char]]]
|
||||
tests = [ [],
|
||||
["ABC"],
|
||||
["ABC","DEF"],
|
||||
["ABC","DEF","G","H"] ]
|
||||
|
||||
quibble :: [[char]]->[char]
|
||||
quibble [] = []
|
||||
quibble [word] = word
|
||||
quibble [word1,word2] = word1 ++ " and " ++ word2
|
||||
quibble (word:words) = word ++ ", " ++ quibble words
|
||||
25
Task/Comma-quibbling/NetRexx/comma-quibbling.netrexx
Normal file
25
Task/Comma-quibbling/NetRexx/comma-quibbling.netrexx
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
runSample(arg)
|
||||
return
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method quibble(arg) public static
|
||||
parse arg '[' lst ']'
|
||||
lst = lst.changestr('"', '').space(1)
|
||||
lc = lst.lastpos(',')
|
||||
if lc > 0 then
|
||||
lst = lst.insert('and', lc).overlay(' ', lc)
|
||||
return '{'lst'}'
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method runSample(arg) private static
|
||||
lists = ['[]', - -- {}
|
||||
'["ABC"]', - -- {ABC}
|
||||
'["ABC", "DEF"]', - -- {ABC and DEF}
|
||||
'["ABC", "DEF", "G", "H"]'] -- {ABC, DEF, G and H}
|
||||
loop lst over lists
|
||||
say lst.right(30) ':' quibble(lst)
|
||||
end lst
|
||||
return
|
||||
10
Task/Comma-quibbling/Nim/comma-quibbling.nim
Normal file
10
Task/Comma-quibbling/Nim/comma-quibbling.nim
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
proc commaQuibble(s: openArray[string]): string =
|
||||
result = ""
|
||||
for i, c in s:
|
||||
if i > 0: result.add (if i < s.high: ", " else: " and ")
|
||||
result.add c
|
||||
result = "{" & result & "}"
|
||||
|
||||
var s = @[@[], @["ABC"], @["ABC", "DEF"], @["ABC", "DEF", "G", "H"]]
|
||||
for i in s:
|
||||
echo commaQuibble(i)
|
||||
17
Task/Comma-quibbling/OCaml/comma-quibbling-1.ocaml
Normal file
17
Task/Comma-quibbling/OCaml/comma-quibbling-1.ocaml
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
open Printf
|
||||
|
||||
let quibble list =
|
||||
let rec aux = function
|
||||
| a :: b :: c :: d :: rest -> a ^ ", " ^ aux (b :: c :: d :: rest)
|
||||
| [a; b; c] -> sprintf "%s, %s and %s}" a b c
|
||||
| [a; b] -> sprintf "%s and %s}" a b
|
||||
| [a] -> sprintf "%s}" a
|
||||
| [] -> "}" in
|
||||
"{" ^ aux list
|
||||
|
||||
let test () =
|
||||
[[];
|
||||
["ABC"];
|
||||
["ABC"; "DEF"];
|
||||
["ABC"; "DEF"; "G"; "H"]]
|
||||
|> List.iter (fun list -> print_endline (quibble list))
|
||||
15
Task/Comma-quibbling/OCaml/comma-quibbling-2.ocaml
Normal file
15
Task/Comma-quibbling/OCaml/comma-quibbling-2.ocaml
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
open Core
|
||||
|
||||
let quibble = function
|
||||
| [| |] -> "{}"
|
||||
| [| a |] -> sprintf "{%s}" a
|
||||
| array ->
|
||||
let last, rest = Array.last array, Array.slice array 0 (-1) in
|
||||
sprintf "{%s and %s}" (String.concat_array ~sep:", " rest) last
|
||||
|
||||
let test () =
|
||||
[[||];
|
||||
[|"ABC"|];
|
||||
[|"ABC"; "DEF"|];
|
||||
[|"ABC"; "DEF"; "G"; "H"|]]
|
||||
|> List.iter ~f:(fun list -> print_endline (quibble list))
|
||||
45
Task/Comma-quibbling/Oberon-2/comma-quibbling.oberon
Normal file
45
Task/Comma-quibbling/Oberon-2/comma-quibbling.oberon
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
MODULE CommaQuibbling;
|
||||
IMPORT
|
||||
NPCT:Args,
|
||||
Strings,
|
||||
Out;
|
||||
|
||||
VAR
|
||||
str: ARRAY 256 OF CHAR;
|
||||
|
||||
PROCEDURE Do(VAR s: ARRAY OF CHAR);
|
||||
VAR
|
||||
aux: ARRAY 128 OF CHAR;
|
||||
i,params: LONGINT;
|
||||
BEGIN
|
||||
params := Args.Number() - 1;
|
||||
CASE params OF
|
||||
0:
|
||||
COPY("{}",s)
|
||||
|1:
|
||||
Args.At(1,aux);
|
||||
Strings.Append("{",s);
|
||||
Strings.Append(aux,s);
|
||||
Strings.Append("}",s);
|
||||
ELSE
|
||||
Strings.Append("{",s);
|
||||
FOR i := 1 TO params - 1 DO
|
||||
Args.At(i,aux);
|
||||
Strings.Append(aux,s);
|
||||
IF i # params - 1 THEN
|
||||
Strings.Append(", ",s)
|
||||
ELSE
|
||||
Strings.Append(" and ", s)
|
||||
END
|
||||
END;
|
||||
Args.At(params,aux);
|
||||
Strings.Append(aux,s);
|
||||
Strings.Append("}",s)
|
||||
END;
|
||||
|
||||
END Do;
|
||||
|
||||
BEGIN
|
||||
Do(str);
|
||||
Out.String(":> ");Out.String(str);Out.Ln
|
||||
END CommaQuibbling.
|
||||
32
Task/Comma-quibbling/Objeck/comma-quibbling.objeck
Normal file
32
Task/Comma-quibbling/Objeck/comma-quibbling.objeck
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
class Quibbler {
|
||||
function : Quibble(words : String[]) ~ String {
|
||||
text := "{";
|
||||
|
||||
each(i : words) {
|
||||
text += words[i];
|
||||
if(i < words->Size() - 2) {
|
||||
text += ", ";
|
||||
}
|
||||
else if(i = words->Size() - 2) {
|
||||
text += " and ";
|
||||
};
|
||||
};
|
||||
text += "}";
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
words := String->New[0];
|
||||
Quibble(words)->PrintLine();
|
||||
|
||||
words := ["ABC"];
|
||||
Quibble(words)->PrintLine();
|
||||
|
||||
words := ["ABC", "DEF"];
|
||||
Quibble(words)->PrintLine();
|
||||
|
||||
words := ["ABC", "DEF", "G", "H"];
|
||||
Quibble(words)->PrintLine();
|
||||
}
|
||||
}
|
||||
9
Task/Comma-quibbling/Oforth/comma-quibbling.fth
Normal file
9
Task/Comma-quibbling/Oforth/comma-quibbling.fth
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
: quibbing(l) -- string
|
||||
| i s |
|
||||
StringBuffer new "{" <<
|
||||
l size dup 1- ->s loop: i [
|
||||
l at(i) <<
|
||||
i s < ifTrue: [ ", " << continue ]
|
||||
i s == ifTrue: [ " and " << ]
|
||||
]
|
||||
"}" << dup freeze ;
|
||||
19
Task/Comma-quibbling/Ol/comma-quibbling.ol
Normal file
19
Task/Comma-quibbling/Ol/comma-quibbling.ol
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
(define (quibble . args)
|
||||
(display "{")
|
||||
(let loop ((args args))
|
||||
(unless (null? args) (begin
|
||||
(display (car args))
|
||||
(cond
|
||||
((= 1 (length args)) #t)
|
||||
((= 2 (length args))
|
||||
(display " and "))
|
||||
(else
|
||||
(display ", ")))
|
||||
(loop (cdr args)))))
|
||||
(print "}"))
|
||||
|
||||
; testing =>
|
||||
(quibble)
|
||||
(quibble "ABC")
|
||||
(quibble "ABC" "DEF")
|
||||
(quibble "ABC" "DEF" "G" "H")
|
||||
11
Task/Comma-quibbling/PARI-GP/comma-quibbling.parigp
Normal file
11
Task/Comma-quibbling/PARI-GP/comma-quibbling.parigp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
comma(v)={
|
||||
if(#v==0, return("{}"));
|
||||
if(#v==1, return(Str("{"v[1]"}")));
|
||||
my(s=Str("{",v[1]));
|
||||
for(i=2,#v-1,s=Str(s,", ",v[i]));
|
||||
Str(s," and ",v[#v],"}")
|
||||
};
|
||||
comma([])
|
||||
comma(["ABC"])
|
||||
comma(["ABC", "DEF"])
|
||||
comma(["ABC", "DEF", "G", "H"])
|
||||
29
Task/Comma-quibbling/PHP/comma-quibbling.php
Normal file
29
Task/Comma-quibbling/PHP/comma-quibbling.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
function quibble($arr){
|
||||
|
||||
$words = count($arr);
|
||||
|
||||
if($words == 0){
|
||||
return '{}';
|
||||
}elseif($words == 1){
|
||||
return '{'.$arr[0].'}';
|
||||
}elseif($words == 2){
|
||||
return '{'.$arr[0].' and '.$arr[1].'}';
|
||||
}else{
|
||||
return '{'.implode(', ', array_splice($arr, 0, -1) ). ' and '.$arr[0].'}';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
$tests = [
|
||||
[],
|
||||
["ABC"],
|
||||
["ABC", "DEF"],
|
||||
["ABC", "DEF", "G", "H"]
|
||||
];
|
||||
|
||||
foreach ($tests as $test) {
|
||||
echo quibble($test) . PHP_EOL;
|
||||
}
|
||||
46
Task/Comma-quibbling/PL-I/comma-quibbling.pli
Normal file
46
Task/Comma-quibbling/PL-I/comma-quibbling.pli
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
*process or(!);
|
||||
quib: Proc Options(main);
|
||||
/*********************************************************************
|
||||
* 06.10.2013 Walter Pachl
|
||||
*********************************************************************/
|
||||
put Edit*process or(!);
|
||||
quib: Proc Options(main);
|
||||
/*********************************************************************
|
||||
* 06.10.2013 Walter Pachl
|
||||
* 07.10.2013 -"- change "Oxford comma" to and
|
||||
*********************************************************************/
|
||||
put Edit(quibbling(''))(Skip,a);
|
||||
put Edit(quibbling('ABC'))(Skip,a);
|
||||
put Edit(quibbling('ABC DEF'))(Skip,a);
|
||||
put Edit(quibbling('ABC DEF G H'))(Skip,a);
|
||||
return;
|
||||
|
||||
quibbling: proc(s) Returns(Char(100) Var);
|
||||
Dcl s Char(*);
|
||||
Dcl result Char(100) Var Init('');
|
||||
Dcl word(10) Char(100) Var;
|
||||
Dcl (wi,p) Bin Fixed(31);
|
||||
If s='' Then result='';
|
||||
Else Do;
|
||||
Do wi=1 By 1 While(s^='');
|
||||
p=index(s,' ');
|
||||
if p=0 Then Do;
|
||||
word(wi)=s;
|
||||
s='';
|
||||
End;
|
||||
Else Do;
|
||||
word(wi)=left(s,p-1);
|
||||
s=substr(s,p+1);
|
||||
End;
|
||||
end;
|
||||
wn=wi-1;
|
||||
result=word(1);
|
||||
Do i=2 To wn-1;
|
||||
result=result!!', '!!word(i);
|
||||
End;
|
||||
If wn>1 Then
|
||||
result=result!!' and '!!word(wn);
|
||||
End;
|
||||
Return('{'!!result!!'}');
|
||||
End;
|
||||
End;
|
||||
74
Task/Comma-quibbling/PL-M/comma-quibbling.plm
Normal file
74
Task/Comma-quibbling/PL-M/comma-quibbling.plm
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
100H:
|
||||
|
||||
/* COPY A STRING (MINUS TERMINATOR), RETURNS LENGTH (MINUS TERMINATOR) */
|
||||
COPY$STR: PROCEDURE(SRC, DST) ADDRESS;
|
||||
DECLARE (SRC, DST) ADDRESS;
|
||||
DECLARE (SCH BASED SRC, DCH BASED DST) BYTE;
|
||||
DECLARE L ADDRESS;
|
||||
L = 0;
|
||||
DO WHILE SCH <> '$';
|
||||
DCH = SCH;
|
||||
SRC = SRC + 1;
|
||||
DST = DST + 1;
|
||||
L = L + 1;
|
||||
END;
|
||||
RETURN L;
|
||||
END COPY$STR;
|
||||
|
||||
/* QUIBBLE GIVEN ARRAY OF $-TERMINATED STRINGS, STORE RESULT IN BUFR */
|
||||
QUIBBLE: PROCEDURE(WORDS, BUFR) ADDRESS;
|
||||
DECLARE (WORDS, BUFR, ADR) ADDRESS;
|
||||
DECLARE (WORD BASED WORDS, WPTR) ADDRESS;
|
||||
DECLARE (WCHAR BASED WPTR, BCHAR BASED BUFR) BYTE;
|
||||
|
||||
/* BRACES AND LOWERCASE LETTERS ARE NOT WITHIN PL/M CHARSET */
|
||||
DECLARE LBRACE LITERALLY '123', RBRACE LITERALLY '125';
|
||||
DECLARE ANDSTR DATA (32,97,110,100,32,'$');
|
||||
|
||||
ADR = BUFR;
|
||||
BCHAR = LBRACE;
|
||||
BUFR = BUFR + 1;
|
||||
DO WHILE WORD <> 0;
|
||||
BUFR = BUFR + COPY$STR(WORD, BUFR);
|
||||
WORDS = WORDS + 2;
|
||||
IF WORD <> 0 THEN
|
||||
IF WORD(1) <> 0 THEN
|
||||
BUFR = BUFR + COPY$STR(.', $', BUFR);
|
||||
ELSE
|
||||
BUFR = BUFR + COPY$STR(.ANDSTR, BUFR);
|
||||
END;
|
||||
BCHAR = RBRACE;
|
||||
BUFR = BUFR + 1;
|
||||
BCHAR = '$';
|
||||
RETURN ADR;
|
||||
END QUIBBLE;
|
||||
|
||||
/* --- CP/M OUTPUT AND TESTING --- */
|
||||
BDOS: PROCEDURE(FUNC, ARG); /* MAKE CP/M SYSTEM CALL */
|
||||
DECLARE FUNC BYTE, ARG ADDRESS;
|
||||
GO TO 5;
|
||||
END BDOS;
|
||||
|
||||
DECLARE BDOS$EXIT LITERALLY '0', /* EXIT TO CP/M */
|
||||
BDOS$PUTS LITERALLY '9'; /* PRINT STRING */
|
||||
|
||||
PUTS: PROCEDURE(S);
|
||||
DECLARE S ADDRESS;
|
||||
CALL BDOS(BDOS$PUTS, S);
|
||||
CALL BDOS(BDOS$PUTS, .(13,10,'$'));
|
||||
END PUTS;
|
||||
|
||||
/* ARRAY WITH INITIALLY NO CONTENTS */
|
||||
DECLARE ARR (5) ADDRESS INITIAL (0,0,0,0,0);
|
||||
|
||||
CALL PUTS(QUIBBLE(.ARR, .MEMORY)); /* NO STRINGS */
|
||||
ARR(0) = .'ABC$';
|
||||
CALL PUTS(QUIBBLE(.ARR, .MEMORY)); /* ABC */
|
||||
ARR(1) = .'DEF$';
|
||||
CALL PUTS(QUIBBLE(.ARR, .MEMORY)); /* ABC AND DEF */
|
||||
ARR(2) = .'G$';
|
||||
ARR(3) = .'H$';
|
||||
CALL PUTS(QUIBBLE(.ARR, .MEMORY)); /* ABC, DEF, G AND H */
|
||||
|
||||
CALL BDOS(BDOS$EXIT, 0);
|
||||
EOF
|
||||
134
Task/Comma-quibbling/Pascal/comma-quibbling.pas
Normal file
134
Task/Comma-quibbling/Pascal/comma-quibbling.pas
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
program CommaQuibbling;
|
||||
|
||||
uses
|
||||
SysUtils,
|
||||
Classes,
|
||||
StrUtils;
|
||||
|
||||
const
|
||||
OuterBracket =['[', ']'];
|
||||
|
||||
type
|
||||
|
||||
{$IFNDEF FPC}
|
||||
SizeInt = LongInt;
|
||||
{$ENDIF}
|
||||
|
||||
|
||||
|
||||
{ TCommaQuibble }
|
||||
|
||||
TCommaQuibble = class(TStringList)
|
||||
private
|
||||
function GetCommaquibble: string;
|
||||
procedure SetCommaQuibble(AValue: string);
|
||||
public
|
||||
property CommaQuibble: string read GetCommaquibble write SetCommaQuibble;
|
||||
end;
|
||||
|
||||
{$IFNDEF FPC} // Delphi support
|
||||
|
||||
function WordPosition(const N: Integer; const S: string; const WordDelims:
|
||||
TSysCharSet): SizeInt;
|
||||
var
|
||||
PS, P, PE: PChar;
|
||||
Count: Integer;
|
||||
begin
|
||||
Result := 0;
|
||||
Count := 0;
|
||||
PS := PChar(pointer(S));
|
||||
PE := PS + Length(S);
|
||||
P := PS;
|
||||
while (P < PE) and (Count <> N) do
|
||||
begin
|
||||
while (P < PE) and (P^ in WordDelims) do
|
||||
inc(P);
|
||||
if (P < PE) then
|
||||
inc(Count);
|
||||
if (Count <> N) then
|
||||
while (P < PE) and not (P^ in WordDelims) do
|
||||
inc(P)
|
||||
else
|
||||
Result := (P - PS) + 1;
|
||||
end;
|
||||
end;
|
||||
|
||||
function ExtractWordPos(N: Integer; const S: string; const WordDelims:
|
||||
TSysCharSet; out Pos: Integer): string;
|
||||
var
|
||||
i, j, l: SizeInt;
|
||||
begin
|
||||
j := 0;
|
||||
i := WordPosition(N, S, WordDelims);
|
||||
if (i > High(Integer)) then
|
||||
begin
|
||||
Result := '';
|
||||
Pos := -1;
|
||||
Exit;
|
||||
end;
|
||||
Pos := i;
|
||||
if (i <> 0) then
|
||||
begin
|
||||
j := i;
|
||||
l := Length(S);
|
||||
while (j <= l) and not (S[j] in WordDelims) do
|
||||
inc(j);
|
||||
end;
|
||||
SetLength(Result, j - i);
|
||||
if ((j - i) > 0) then
|
||||
Result := copy(S, i, j - i);
|
||||
end;
|
||||
|
||||
function ExtractWord(N: Integer; const S: string; const WordDelims: TSysCharSet):
|
||||
string; inline;
|
||||
var
|
||||
i: SizeInt;
|
||||
begin
|
||||
Result := ExtractWordPos(N, S, WordDelims, i);
|
||||
end;
|
||||
{$ENDIF}
|
||||
|
||||
{ TCommaQuibble }
|
||||
|
||||
procedure TCommaQuibble.SetCommaQuibble(AValue: string);
|
||||
begin
|
||||
AValue := ExtractWord(1, AValue, OuterBracket);
|
||||
commatext := AValue;
|
||||
end;
|
||||
|
||||
function TCommaQuibble.GetCommaquibble: string;
|
||||
var
|
||||
x: Integer;
|
||||
Del: string;
|
||||
begin
|
||||
result := '';
|
||||
Del := ', ';
|
||||
for x := 0 to Count - 1 do
|
||||
begin
|
||||
result := result + Strings[x];
|
||||
if x = Count - 2 then
|
||||
Del := ' and '
|
||||
else if x = Count - 1 then
|
||||
Del := '';
|
||||
result := result + Del;
|
||||
end;
|
||||
result := '{' + result + '}';
|
||||
end;
|
||||
|
||||
const
|
||||
TestData: array[0..7] of string = ('[]', '["ABC"]', '["ABC", "DEF"]',
|
||||
'["ABC", "DEF", "G", "H"]', '', '"ABC"', '"ABC", "DEF"', '"ABC", "DEF", "G", "H"');
|
||||
|
||||
var
|
||||
Quibble: TCommaQuibble;
|
||||
TestString: string;
|
||||
|
||||
begin
|
||||
Quibble := TCommaQuibble.Create;
|
||||
|
||||
for TestString in TestData do
|
||||
begin
|
||||
Quibble.CommaQuibble := TestString;
|
||||
writeln(Quibble.CommaQuibble);
|
||||
end;
|
||||
end.
|
||||
8
Task/Comma-quibbling/Perl/comma-quibbling-1.pl
Normal file
8
Task/Comma-quibbling/Perl/comma-quibbling-1.pl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
sub comma_quibbling :prototype(@) {
|
||||
return "{$_}" for
|
||||
@_ < 2 ? "@_" :
|
||||
join(', ', @_[0..@_-2]) . ' and ' . $_[-1];
|
||||
}
|
||||
|
||||
print comma_quibbling(@$_), "\n" for
|
||||
[], [qw(ABC)], [qw(ABC DEF)], [qw(ABC DEF G H)];
|
||||
8
Task/Comma-quibbling/Perl/comma-quibbling-2.pl
Normal file
8
Task/Comma-quibbling/Perl/comma-quibbling-2.pl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
use 5.01;
|
||||
sub comma_quibbling {
|
||||
my $last = pop // '';
|
||||
return '{'. (@_ ? (join ', ', @_).' and '.$last : $last).'}';
|
||||
}
|
||||
|
||||
say for map {comma_quibbling(@$_)}
|
||||
[], [qw(ABC)], [qw(ABC DEF)], [qw(ABC DEF G H)];
|
||||
17
Task/Comma-quibbling/Phix/comma-quibbling.phix
Normal file
17
Task/Comma-quibbling/Phix/comma-quibbling.phix
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">quibble</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)>=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">words</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]&</span><span style="color: #008000;">" and "</span><span style="color: #0000FF;">&</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]}</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #008000;">"{"</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">,</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"}"</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #008000;">"ABC"</span><span style="color: #0000FF;">},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #008000;">"ABC"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"DEF"</span><span style="color: #0000FF;">},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #008000;">"ABC"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"DEF"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"G"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"H"</span><span style="color: #0000FF;">}}</span>
|
||||
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">quibble</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<!--
|
||||
8
Task/Comma-quibbling/PicoLisp/comma-quibbling.l
Normal file
8
Task/Comma-quibbling/PicoLisp/comma-quibbling.l
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(for L '([] ["ABC"] ["ABC", "DEF"] ["ABC", "DEF", "G", "H"])
|
||||
(let H (head -1 L)
|
||||
(prinl
|
||||
"{"
|
||||
(glue ", " H)
|
||||
(and H " and ")
|
||||
(last L)
|
||||
"}" ) ) )
|
||||
44
Task/Comma-quibbling/Plain-English/comma-quibbling.plain
Normal file
44
Task/Comma-quibbling/Plain-English/comma-quibbling.plain
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
To quibble four words:
|
||||
Add "ABC" to some string things.
|
||||
Add "DEF" to the string things.
|
||||
Add "G" to the string things.
|
||||
Add "H" to the string things.
|
||||
Quibble the string things.
|
||||
|
||||
To quibble one word:
|
||||
Add "ABC" to some string things.
|
||||
Quibble the string things.
|
||||
|
||||
To quibble some string things:
|
||||
Quibble the string things giving a string.
|
||||
Destroy the string things.
|
||||
Write the string on the console.
|
||||
|
||||
To quibble some string things giving a string:
|
||||
Append "{" to the string.
|
||||
Put the string things' count into a count.
|
||||
If the count is 0, append "}" to the string; exit.
|
||||
Get a string thing from the string things.
|
||||
If the count is 1, append the string thing's string then "}" to the string; exit.
|
||||
Loop.
|
||||
If a counter is past the count minus 2, append the string thing's string then " and " then the string thing's next's string then "}" to the string; exit.
|
||||
Append the string thing's string then ", " to the string.
|
||||
Put the string thing's next into the string thing.
|
||||
Repeat.
|
||||
|
||||
To quibble two words:
|
||||
Add "ABC" to some string things.
|
||||
Add "DEF" to the string things.
|
||||
Quibble the string things.
|
||||
|
||||
To quibble zero words:
|
||||
Quibble some string things.
|
||||
|
||||
To run:
|
||||
Start up.
|
||||
Quibble zero words.
|
||||
Quibble one word.
|
||||
Quibble two words.
|
||||
Quibble four words.
|
||||
Wait for the escape key.
|
||||
Shut down.
|
||||
41
Task/Comma-quibbling/PowerShell/comma-quibbling-1.psh
Normal file
41
Task/Comma-quibbling/PowerShell/comma-quibbling-1.psh
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
function Out-Quibble
|
||||
{
|
||||
[OutputType([string])]
|
||||
Param
|
||||
(
|
||||
# Zero or more strings.
|
||||
[Parameter(Mandatory=$false, Position=0)]
|
||||
[AllowEmptyString()]
|
||||
[string[]]
|
||||
$Text = ""
|
||||
)
|
||||
|
||||
# If not null or empty...
|
||||
if ($Text)
|
||||
{
|
||||
# Remove empty strings from the array.
|
||||
$text = "$Text".Split(" ", [StringSplitOptions]::RemoveEmptyEntries)
|
||||
}
|
||||
else
|
||||
{
|
||||
return "{}"
|
||||
}
|
||||
|
||||
# Build a format string.
|
||||
$outStr = ""
|
||||
for ($i = 0; $i -lt $text.Count; $i++)
|
||||
{
|
||||
$outStr += "{$i}, "
|
||||
}
|
||||
$outStr = $outStr.TrimEnd(", ")
|
||||
|
||||
# If more than one word, insert " and" at last comma position.
|
||||
if ($text.Count -gt 1)
|
||||
{
|
||||
$cIndex = $outStr.LastIndexOf(",")
|
||||
$outStr = $outStr.Remove($cIndex,1).Insert($cIndex," and")
|
||||
}
|
||||
|
||||
# Output the formatted string.
|
||||
"{" + $outStr -f $text + "}"
|
||||
}
|
||||
4
Task/Comma-quibbling/PowerShell/comma-quibbling-2.psh
Normal file
4
Task/Comma-quibbling/PowerShell/comma-quibbling-2.psh
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
Out-Quibble
|
||||
Out-Quibble "ABC"
|
||||
Out-Quibble "ABC", "DEF"
|
||||
Out-Quibble "ABC", "DEF", "G", "H"
|
||||
11
Task/Comma-quibbling/PowerShell/comma-quibbling-3.psh
Normal file
11
Task/Comma-quibbling/PowerShell/comma-quibbling-3.psh
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
$file = @'
|
||||
|
||||
ABC
|
||||
ABC, DEF
|
||||
ABC, DEF, G, H
|
||||
'@ -split [Environment]::NewLine
|
||||
|
||||
foreach ($line in $file)
|
||||
{
|
||||
Out-Quibble -Text ($line -split ", ")
|
||||
}
|
||||
18
Task/Comma-quibbling/Prolog/comma-quibbling-1.pro
Normal file
18
Task/Comma-quibbling/Prolog/comma-quibbling-1.pro
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
words_series(Words, Bracketed) :-
|
||||
words_serialized(Words, Serialized),
|
||||
atomics_to_string(["{",Serialized,"}"], Bracketed).
|
||||
|
||||
words_serialized([], "").
|
||||
words_serialized([Word], Word) :- !.
|
||||
words_serialized(Words, Serialized) :-
|
||||
append(Rest, [Last], Words), %% Splits the list of *Words* into the *Last* word and the *Rest*
|
||||
atomics_to_string(Rest, ", ", WithCommas),
|
||||
atomics_to_string([WithCommas, " and ", Last], Serialized).
|
||||
|
||||
|
||||
|
||||
test :-
|
||||
forall( member(Words, [[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]]),
|
||||
( words_series(Words, Series),
|
||||
format('~w ~15|=> ~w~n', [Words, Series]))
|
||||
).
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue