CJam - 142 / GolfScript - 144
{" had| a |farm|68, |68 8|here|Old MacDonald765|, E-I-E-I-O|10,
And on that 5 he7690,
With3 2 and3 t2,
Here4t24everyw23,
10!"'|/~A,{`/\*}/}:F;
Usage: "cow""moo"F
For GolfScript replace '|
with "|"
and A
with 10
Explanation:
The key part is A,{`/\*}/
:
A,{...}/
executes the block for each number from 0 to 9 (A=10)
`
converts the number to string
/\*
does a string replace: if we have on the stack "bar" "foo 1 baz" "1"
then /
splits the string resulting in ["foo " " baz"]
, \
swaps this array with the previous item ("bar") and *
joins the array resulting in "foo bar baz"
So the code replaces each number in the main string with a string that's previously on the stack. We have the animal and the sound, then " had", " a ", etc and finally ", E-I-E-I-O" and the main string, "10,...!". To avoid using too many quotes, I put all the strings (except the parameters) in one string, then split it and dumped the resulting array ('|/~
)
The main string goes through the following transformations:
10,
And on that 5 he7690,
With3 2 and3 t2,
Here4t24everyw23,
10!
replace "0"
with ", E-I-E-I-O"
:
1, E-I-E-I-O,
And on that 5 he769, E-I-E-I-O,
With3 2 and3 t2,
Here4t24everyw23,
1, E-I-E-I-O!
replace "1"
with "Old MacDonald765"
:
Old MacDonald765, E-I-E-I-O,
And on that 5 he769, E-I-E-I-O,
With3 2 and3 t2,
Here4t24everyw23,
Old MacDonald765, E-I-E-I-O!
replace "2"
with "here"
, then "3"
with "68 8"
etc.
8 corresponds to the sound, and 9 to the animal.
1Shouldn't this be [tag:kolmogorov-complexity] though? – mniip – 2014-05-05T09:28:24.053
6You say
echoes the following
. Do you mean the function should print it out or return it? – cjfaure – 2014-05-05T09:31:23.5272To make this fair, I think this should have the exact punctuation, spaces and carriage returns in the question. But what do you think about uppercase / lowercase? I thought a single case might be easier and avoid a load of standard applications of base64 or similar. On balance, the only capitals are at the beginning of the line, the word MacDonald and in the E-I-E-I-O, so it may be more interesting to make that exactly per the question too. – Level River St – 2014-05-05T09:45:01.987
Also, given the use of "sclipting" in similar questions, please clarify if this is per Unicode character, ASCII character, or byte. – Level River St – 2014-05-05T09:54:01.023
1Punctuation is essential, as are capitals. The function should print it out. – rybo111 – 2014-05-05T10:20:16.453
4Is it acceptable to output
a oink
or should that be made to bean oink
? – ClickRick – 2014-05-05T16:06:47.817@ClickRick A pedant may object and the same could be said for examples like
some chicks
-- perhaps make a "pedants version" which caters for words ending withs
and/or beginning with a vowel. – rybo111 – 2014-05-05T17:02:29.127@rybo111 re pedants version - what about plurals that don't end in "s" - e.g. some sheep or some geese? – Digital Trauma – 2014-05-05T17:29:28.947
@DigitalTrauma good point - I guess the pedants version would need some consideration -- perhaps determine both words in the variable, or have a predefined list of animals and their respective sounds! – rybo111 – 2014-05-05T17:43:30.733
4
@rybo111: Were you aware that the great Donald Knuth wrote an academic paper about this kind of thing? It was actually published in a real journal (admittedly in the April edition). More here including a link to the PDF of the paper: http://en.wikipedia.org/wiki/The_Complexity_of_Songs
– Tom Chantler – 2014-05-06T16:11:14.4731@Dommer No, I wasn't aware. Interesting! – rybo111 – 2014-05-06T17:42:57.017