Old lady swallowed a fly: Difference between revisions
(→{{header|C}}: shorter yet) |
m (→{{header|C}}: ok done with it) |
||
Line 6: | Line 6: | ||
char *dict[] = { |
char *dict[] = { |
||
"\ |
"\x6""fly,\x7why\x3\xf,/That wr\xc""and w\xc" |
||
"and t\ |
"and t\xcinside her;\x1""bird;/How absurd\xa" |
||
" |
"bird!\xb\x1""cat;/Fancy that\xa""cat!\x5\x9 \x3" |
||
"dog;/What a hog\xa""dog;\xd""cow,\x7how\xe""cow;\x2" |
|||
"cow\x4""dog,\xdhorse.../She's dead, of course!/", |
|||
"cow,\x6w how she\x7\xf""cow;\x3""cow\x4""dog," |
|||
"\ |
"\x9\x3", "/She\x8""ed the ", "\xe" |
||
"fly - Perhaps she'll die!//\x6", " to catch the ", |
|||
"\ |
"\x2""cat\x4""bird,\xb", "There was an old lady who\x8" |
||
" |
"ed a ", "/I don't know ", " swallow", "\x2\xf\x4" |
||
" |
"fly;\x7why", ", to\x8 a ", "\x2""bird\x4\xf,", "iggled ", |
||
"\ |
"\x2""dog\x4""cat,\x5\x1", " she\x8""ed a ", "spider" |
||
"\x3""dog\x4""cat,\x9\x1", "ed a ", "\x4""fly;\x6", "bird", |
|||
}; |
}; |
||
Revision as of 05:51, 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[] = { "\x6""fly,\x7why\x3\xf,/That wr\xc""and w\xc" "and t\xcinside her;\x1""bird;/How absurd\xa" "bird!\xb\x1""cat;/Fancy that\xa""cat!\x5\x9 \x3" "dog;/What a hog\xa""dog;\xd""cow,\x7how\xe""cow;\x2" "cow\x4""dog,\xdhorse.../She's dead, of course!/", "\x9\x3", "/She\x8""ed the ", "\xe" "fly - Perhaps she'll die!//\x6", " to catch the ", "\x2""cat\x4""bird,\xb", "There was an old lady who\x8" "ed a ", "/I don't know ", " swallow", "\x2\xf\x4" "fly;\x7why", ", to\x8 a ", "\x2""bird\x4\xf,", "iggled ", "\x2""dog\x4""cat,\x5\x1", " she\x8""ed a ", "spider" };
void writeout(char *s) { do { if (*s < 32) writeout(dict[(int)*s]); else putchar(*s == '/' ? '\n' : *s); } while (*++s); }
int main() { writeout(dict[0]); return 0; }</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>