Old lady swallowed a fly: Difference between revisions
(J - based on wikipedia) |
m (→{{header|J}}) |
||
Line 58: | Line 58: | ||
'Cow'N'I don''t know how' |
'Cow'N'I don''t know how' |
||
'Donkey'N'It was rather wonky' |
'Donkey'N'It was rather wonky' |
||
O'Horse' |
|||
'cow'N'I don''t know how' |
|||
O'horse' |
|||
E'She''s dead, of course!'</lang> |
E'She''s dead, of course!'</lang> |
||
Revision as of 21:31, 17 August 2011
Present a program which emits the lyrics to the song I Knew an Old Lady Who Swallowed a Fly, taking advantage of the repetitive structure of the song's lyrics.
C
Boring, ad hoc dictionary based decompression. The encoder was arguably more interesting, though. <lang c>#include <stdio.h>
char *dict[] = { "_fa fly,_c _m,/That wr_kand w_kand t_kinside her;_`_n;/" "How absurd_g_n!_h`cat;/Fancy that_gcat!_ddog;/What a" " hog_gdog!_jcow,_ihow s_la cow;_acow_bdog,_jhorse.../" "She's dead, of course!/", "_a_m_bfly;_c ", "/S_lthe ", " to catch the ", "_iwhy s_la fly - Perhaps she'll die!//_fa", "_acat_b_n,_h`", " swallow", "There was an old lady who_eed ", ", to_e a ", "_a_n_b_m,_", "/I don't know ", "_adog_bcat,_d", "iggled ", "he_eed ", "spider", "bird", };
int print(char *c, int s) { do { if (s) s = print(dict[*c - 95], 0); else if (*c == '_') s = 1; else putchar(*c == '/' ? '\n' : *c); } while (*++c); return s; }
int main() { print(dict[0], 0); return 0; }</lang>
J
This defines T to be the required text.
<lang j>T=: e=:3 :'T=:T,y,LF' E=:e@,&'.' O=:'I know an old lady who swallowed a 'E@,] I=:e bind('I dont know why she swallowed the fly.',LF,'Perhaps shell die.',LF) I O 'fly' O P=:'spider' E 'That wriggled and jiggled and tickled inside her' I E A=:'She swallowed the spider to catch the fly' N=:4 :0
O x E y,'. To swallow a ',x I E A=:'She swallowed the ',x,' to catch the ',P,'.',LF,A P=:x
) 'Bird'N'Quite absurd' 'Cat'N'Fancy that' 'Dog'N'What a hog' 'Pig'N'Her mouth was so big' 'Goat'N'She just opened her throat' 'Cow'N'I dont know how' 'Donkey'N'It was rather wonky' O'Horse' E'Shes dead, of course!'</lang>
PHP
<lang php><?php
$swallowed = array(
array('swallowed' => 'fly.', 'reason' => "I don't know why she swallowed the fly."), array('swallowed' => 'spider,', 'aside' => "which wiggled and jiggled and tickled inside her.", 'reason' => "She swallowed the spider to catch the fly"), array('swallowed' => 'bird.', 'aside' => "How absurd! To swallow a bird!", 'reason' => "She swallowed the bird to catch the spider,"), array('swallowed' => 'cat.', 'aside' => "Imagine that! To swallow a cat!", 'reason' => "She swallowed the cat to catch the bird."), array('swallowed' => 'dog.', 'aside' => "What a hog! To swallow a dog!", 'reason' => "She swallowed the dog to catch the cat."), array('swallowed' => 'horse', 'aside' => "She's dead, of course. She swallowed a horse!", 'reason' => "She swallowed the horse to catch the dog."));
foreach($swallowed as $creature) {
print "I knew an old lady who swallowed a " . $creature['swallowed'] . "\n"; if(array_key_exists('aside', $creature)) print $creature['aside'] . "\n";
$reversed = array_reverse($swallowed); $history = array_slice($reversed, array_search($creature, $reversed));
foreach($history as $note) { print $note['reason'] . "\n"; }
if($swallowed[count($swallowed) - 1] == $creature) print "But she sure died!\n"; else print "Perhaps she'll die." . "\n\n";
}</lang>