Old lady swallowed a fly: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|C}}: shorter code)
(→‎{{header|C}}: shorter yet)
Line 6: Line 6:


char *dict[] = {
char *dict[] = {
"\x5""fly,\x6\xc\x1\xa,/That wr\xd""and w\xd"
"\x5""fly,\x6\xd\x1\xa,/That wr\xb""and w\xb"
"and t\xdinside her;\x3\xa\xe\xc\x8 //\x5\x10;/"
"and t\xbinside her;\x3\xa\x10\xd\x8 //\x5\x11;/"
"How absurd\x11\x10.\x2\xc\x1\xf;/Fancy that,\x11\xf!"
"How absurd\xc\x11.\x2\xd\x1""cat;/Fancy that,\xc"
"\x9 \x1\x12;/What\xbhog,\x11\x12;\x3\x12\x4\xf,\x9\x1"
"cat!\x9 \x1""dog;/What a hog,\xc""dog;\xe"
"cow,\x6w how s\x13\xb""cow;\x3""cow\x4\x12,\x3\x12\x4\xf,"
"cow,\x6w how she\x7\xf""cow;\x3""cow\x4""dog,"
"\x9\x1horse.../She's dead, of course!/",
"\xehorse.../She's dead, of course!/",


"\x8//\x5", "\x3\x10\x4\xa,\x3\xa\xe", "/S\x13 the ",
"\x8//\x5", "\x3\x11\x4\xa,\x3\xa\x10", "/She\x7""ed the ",
" to \xf""ch the ", "There was an old lady who\x7""ed\xb",
" to catch the ", "There was an old lady who\x7\xf",
"/I don't kno", " swallow", " s\x13\xb""fly - Perhaps she'll die!",
"/I don't kno", " swallow", " she\x7\xf""fly - Perhaps she'll die!",
"\x3\xf\x4\x10,\x2\xc", "spider", " a ", "w why", "iggled ",
"\x3""cat\x4\x11,\x2\xd", "spider", "iggled ", " to\x7 a ", "w why",
"\x4""fly;\x6", "cat", "bird", " to\x7\xb", "dog", "he\x7""ed",
"\x3""dog\x4""cat,\x9\x1", "ed a ", "\x4""fly;\x6", "bird",
};
};



Revision as of 05:38, 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.

Old lady swallowed a fly is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

C

Boring, ad hoc dictionary based decompression. The encoder was arguably more interesting, though. <lang c>#include <stdio.h>

char *dict[] = { "\x5""fly,\x6\xd\x1\xa,/That wr\xb""and w\xb" "and t\xbinside her;\x3\xa\x10\xd\x8 //\x5\x11;/" "How absurd\xc\x11.\x2\xd\x1""cat;/Fancy that,\xc" "cat!\x9 \x1""dog;/What a hog,\xc""dog;\xe" "cow,\x6w how she\x7\xf""cow;\x3""cow\x4""dog," "\xehorse.../She's dead, of course!/",

"\x8//\x5", "\x3\x11\x4\xa,\x3\xa\x10", "/She\x7""ed the ", " to catch the ", "There was an old lady who\x7\xf", "/I don't kno", " swallow", " she\x7\xf""fly - Perhaps she'll die!", "\x3""cat\x4\x11,\x2\xd", "spider", "iggled ", " to\x7 a ", "w why", "\x3""dog\x4""cat,\x9\x1", "ed a ", "\x4""fly;\x6", "bird", };

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

This example is incorrect. Please fix the code and remove this message.

Details: The program does not produce the correct lyrics. (See talk page)

<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>