Old lady swallowed a fly: Difference between revisions

From Rosetta Code
Content added Content deleted
(incorrect)
Line 1: Line 1:
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.{{draft task}}
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.{{draft task}}


=={{header|C}}==
Boring, ad hoc dictionary based decompression. The encoder was arguably more interesting, though.
<lang c>#include <stdio.h>
#include <string.h>

char *dict[] = {
"\x1\x3,/\x1a\x4\xdThat w\xfw\xft\xeinside her;/\x13;/\x1a"
"\x4;/How absurd\x19\x5./\x14;/\x1a\x6;/Fancy that,\x19\x6!/\x15;/\x1a"
"\x7;/What a hog,\x19\x7!/\x16;/\x1a\x7;/\x18how, she\x19\x8;/\x17;/\x1a"
"\x9.../She's dead, of course!/",
"There was an old lady who""\x2""a ", "\x10""d ",
"fly", "spider", "bird", "cat", "dog", "cow", "horse",
" to catch the ", "\x18why she\x2""a ", "\x3 - Perhaps she'll die!//", ",/",
"iggled ", "\xe""and ", " swallow", "She\x2the ", "\xb\xc",
"\x11\x4\xa\x3",
"\x11\x5\xa\x4\xd\x13",
"\x11\x6\xa\x5\xd\x14",
"\x11\x7\xa\x6\xd\x15",
"\x11\x8\xa\x7\xd\x16",
"I don't know ", " to\x10 a ", "\x12\x1",
};

void write_out(char *s)
{
while (*s) {
if (*s == '/') putchar('\n');
else if (*s >= 32) putchar(*s);
else write_out(dict[(int)*s]);

s++;
}
}

int main()
{
write_out(dict[0]);
return 0;
}</lang>
=={{header|PHP}}==
=={{header|PHP}}==
{{incorrect|PHP|The program does not produce the correct lyrics. (See talk page)}}
{{incorrect|PHP|The program does not produce the correct lyrics. (See talk page)}}

Revision as of 02:18, 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>

  1. include <string.h>

char *dict[] = { "\x1\x3,/\x1a\x4\xdThat w\xfw\xft\xeinside her;/\x13;/\x1a" "\x4;/How absurd\x19\x5./\x14;/\x1a\x6;/Fancy that,\x19\x6!/\x15;/\x1a" "\x7;/What a hog,\x19\x7!/\x16;/\x1a\x7;/\x18how, she\x19\x8;/\x17;/\x1a" "\x9.../She's dead, of course!/", "There was an old lady who""\x2""a ", "\x10""d ", "fly", "spider", "bird", "cat", "dog", "cow", "horse", " to catch the ", "\x18why she\x2""a ", "\x3 - Perhaps she'll die!//", ",/", "iggled ", "\xe""and ", " swallow", "She\x2the ", "\xb\xc", "\x11\x4\xa\x3", "\x11\x5\xa\x4\xd\x13", "\x11\x6\xa\x5\xd\x14", "\x11\x7\xa\x6\xd\x15", "\x11\x8\xa\x7\xd\x16", "I don't know ", " to\x10 a ", "\x12\x1", };

void write_out(char *s) { while (*s) { if (*s == '/') putchar('\n'); else if (*s >= 32) putchar(*s); else write_out(dict[(int)*s]);

s++; } }

int main() { write_out(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>