29
\$\begingroup\$

Your goal is to write a program that prints the following poem exactly as it appears here:

There was an old lady who swallowed a fly.
I don't know why she swallowed that fly,
Perhaps she'll die.

There was an old lady who swallowed a spider,
That wriggled and iggled and jiggled inside her.
She swallowed the spider to catch the fly,
I don't know why she swallowed that fly,
Perhaps she'll die.

There was an old lady who swallowed a bird,
How absurd to swallow a bird.
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly,
I don't know why she swallowed that fly,
Perhaps she'll die.

There was an old lady who swallowed a cat,
Imagine that to swallow a cat.
She swallowed the cat to catch the bird,
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly,
I don't know why she swallowed that fly,
Perhaps she'll die.

There was an old lady who swallowed a dog,
What a hog to swallow a dog.
She swallowed the dog to catch the cat,
She swallowed the cat to catch the bird,
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly,
I don't know why she swallowed that fly,
Perhaps she'll die.

There was an old lady who swallowed a horse,
She died of course.

The text must appear exactly as it does here, and fewest characters wins.

Edit: Your program may not access the internet.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Counted in UTF-8 encoded bytes? \$\endgroup\$
    – J B
    Commented Sep 18, 2011 at 18:39
  • \$\begingroup\$ The golf clearly says "fewest characters", not bytes. Encode it how you want as long as it is still a valid program. \$\endgroup\$ Commented Sep 19, 2011 at 20:07
  • 1
    \$\begingroup\$ For reference, the poem is 1208 characters long. \$\endgroup\$
    – J B
    Commented Sep 21, 2011 at 8:50

31 Answers 31

17
\$\begingroup\$

Perl 5.10, 392 384 372 235 369 (ASCII) / 234 (Unicode)

The shortest ASCII version of this is 369 characters long:

@_=(fly,spider,bird,cat,dog);$_="There was an old lady who!ed a";for$P("",",
That wrJand Jand jJinside her",",
How absurd&",",
Imagine that&",",
What a hog&"){$p=$c;$c=$".shift@_;$t=$p?"She!ed the$c to catch the$p,
$t":"I don't know why she!ed that$c,
Perhaps she'll die.

$_";$_.="$c$P.
$t";s/&/ to! a$c/}s/!/ swallow/g;s/J/iggled /g;say"$_ horse,
She died of course."

It started from this base program:

my @animals = qw(fly spider bird cat dog);
my $buf = "There was an old lady who swallowed a ";
for my $phrase ( "",
                 ",\nThat wriggled and iggled and jiggled inside her",
                 ",\nHow absurd&",
                 ",\nImagine that&",
                 ",\nWhat a hog&" ) { 
  $previous = $current;
  $current = shift @animals;
  $trail = $previous ? "She swallowed the $current to catch the $previous,\n$trail"
                     : "I don't know why she swallowed that $current,\n"
                       . "Perhaps she'll die.\n\n$buf";
  $buf .= "$current$phrase.\n$trail";
  $buf =~ s/&/ to swallow a $current/;
}
say "$buf horse,\nShe died of course.";

The core idea is to keep the end of the rhyme and the beginning of the next one in $trail, augmenting it as we go along. It's made non-trivial by the need of a special case for the first use, and the attempt to re-use the animal name variable even in the animal-specific phrase. Further optimizations include:

  • one-character identifiers for everything
  • using barewords instead of quoted strings for the animal list
  • use of the accumulator $_ for $buf to shorten most substitution operations even more (use of @_ is by force of habit and doesn't win anything more than any other character)
  • including the preceding space directly inside the animal name variable (the space character taken from the $" variable)
  • regexp substitution to shorten the most common phrases: ' swallow' and 'iggled '
  • no code spacing whatsoever and all \n in string literals replaced with actual newlines

All but the last optimization yield this:

@_ = (fly, spider, bird, cat, dog);
$_ = "There was an old lady who!ed a";
for $P ( "",
         ",\nThat wrJand Jand jJinside her",
         ",\nHow absurd&",
         ",\nImagine that&",
         ",\nWhat a hog&" ) { 
  $p = $c;
  $c = $" . shift @_;
  $t = $p ? "She!ed the$c to catch the$p,\n$t"
          : "I don't know why she!ed that$c,\nPerhaps she'll die.\n\n$_";
  $_ .= "$c$P.\n$t";
  s/&/ to! a$c/;
}
s/!/ swallow/g;
s/J/iggled /g;
say "$_ horse,\nShe died of course.";

Additionally, this golf is a victim of the underspecified encoding problem. As it—as of now—counts individual characters instead of bytes in a specified encoding, there's a big gain to be achieved by decoding the program source from UCS2 before starting. The final result isn't very readable anymore, but it's short all right. (234 characters, counted as a difference from perl -E'' as usual) (I had to include the trailing newline back to make it valid UCS2)

$ perl -MEncode=from_to -e'$_="䁟㴨晬礬獰楤敲Ɫ楲搬捡琬摯朩㬤弽≔桥牥⁷慳⁡渠潬搠污摹⁷桯Ⅵ搠愢㭦潲⑐⠢∬∬੔桡琠睲䩡湤⁊慮搠橊楮獩摥⁨敲∬∬ੈ潷⁡扳畲搦∬∬੉浡杩湥⁴桡琦∬∬੗桡琠愠桯朦∩笤瀽④㬤挽␢⹳桩晴䁟㬤琽⑰㼢卨攡敤⁴桥④⁴漠捡瑣栠瑨攤瀬ਤ琢㨢䤠摯渧琠歮潷⁷桹⁳桥Ⅵ搠瑨慴④Ⰺ健牨慰猠獨攧汬⁤楥⸊ਤ弢㬤弮㴢④⑐⸊⑴∻猯☯⁴漡⁡④⽽猯ℯ⁳睡汬潷⽧㭳⽊⽩杧汥搠⽧㭳慹∤张桯牳攬੓桥⁤楥搠潦⁣潵牳攮∊";from_to($_,utf8,ucs2);eval'

A good thing there was a lot to golf from before resorting to Unicode, or it wouldn't be much fun.

Edit: can't find a way to copy/paste the 234-character version to this browser, so I'm leaving the 235-character one. Will fix this evening, when I get my hands on a real UTF8-aware clipboard. found a way. Quasi-proof on ideone.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ I revoked my upvote. The unicode packing trick is getting old. \$\endgroup\$
    – Joey Adams
    Commented Sep 20, 2011 at 15:17
  • 2
    \$\begingroup\$ The non-packed version is still in, you know ;p \$\endgroup\$
    – J B
    Commented Sep 20, 2011 at 16:27
  • 5
    \$\begingroup\$ @JoeyAdams really? I didn't even notice the packed version until I read your comment \$\endgroup\$
    – Cyoce
    Commented Jan 6, 2017 at 21:55
9
\$\begingroup\$

Perl, 120 94 chars

Count includes call to the interpreter.

You did say to reproduce it exactly as it does here ;)

perl -MLWP::Simple -E'($p=get("http://goo.gl/kg17j"))=~s#^.+?pr.*?de>|</co.*?re>.+$##sg;say$p'

N.B.

This solution is what prompted the 'no-Internet' restriction. Let it be a lesson for future code-golf question specifications :)

\$\endgroup\$
12
  • 3
    \$\begingroup\$ This entry turned into a quine the moment I posted it! A little regex manipulation restored service back to normal... \$\endgroup\$
    – Zaid
    Commented Sep 18, 2011 at 20:05
  • 3
    \$\begingroup\$ Rotten way to solve it, because it relies on the existence of an outside resource. I'd count the length of the question against it same as I would a program that read the poem from a file. \$\endgroup\$ Commented Sep 19, 2011 at 0:32
  • 3
    \$\begingroup\$ Outside resources are not problem, since it is quite questionable what is resource and what is environment at all. I like exploit of gap in not strictly defined initial question. This is not a solution, but good example to take a care asking questions :-) \$\endgroup\$
    – Ante
    Commented Sep 19, 2011 at 9:14
  • 4
    \$\begingroup\$ It may be a rotten way to solve it, but it relies on an outside resource that's exactly as liable to exist as the question it answers. Which makes it perfectly acceptable as an answer to said question, IMO. \$\endgroup\$
    – J B
    Commented Sep 19, 2011 at 13:09
  • 2
    \$\begingroup\$ @trinithis : The 'no-Internet' restriction was imposed after my post. Admittedly, this is not a serious attempt at coding a solution, but it met the specifications at the time of posting it. Oh, and LWP::Simple is part of the Perl core, at least on v5.12.3. \$\endgroup\$
    – Zaid
    Commented Sep 20, 2011 at 3:17
5
\$\begingroup\$

Vim, 373 keystrokes

iThere was an old lady who swallowed a fly.
I don't know why she s<C-n> that f<C-n>,
Perhaps she'll die.

T<C-x><C-l><Left><C-w>spider<Esc>oThat wriggled <Esc>6hiand <Esc>3hy$u$ppbij<End>inside her.
She s<C-n> the sp<C-n> to catch the f<C-n>,
I<C-x><C-l><C-x><C-l><Esc>qqyapGp3jYPkk$Bqcwbird<Esc>+CHow absurd to sw<C-n><Backspace><Backspace> a b<C-n>.<Esc>qwyb5wvep$Bvepq@qcwcat<Esc>+CImagine that to sw<C-p> a cat.<Esc>@w@qcwdog<Esc>+CWhat a hog to sw<C-p> a dog.<Esc>@w@qcwhorse<Esc>+cGShe d<C-p>d of course.

Funny how exponential that scrambling is.

\$\endgroup\$
4
\$\begingroup\$

Python 3.x: 407 chars

import base64,zlib;print(zlib.decompress(base64.b64decode(b'eNrlkLFOxDAQRPt8xXTXoHwHdEhEot7L+myD8Z7snKz8Pd6LT7mgFFQIRGftPs/OzOBMMiiUQRESGIF4RnGCXCgEKYZBOIW5B7onsMTDhPcopTIzsjN33ORoUvShos8mOTpnJQ4hgL3pu2741rF89mySigwqWJK3NugmMu6eb+3tY648qrRafPniyDQ5TIKRptFdZ83kz+Q5+sQq8ViP0DFfEquZRrT9vnXdbI2v3fzCoPXs9dgHWR/NorpJWoH9oONCrr5vnf35TlisKryqGsGJ3TZS1/uN8EKurlu5/6k7Jymbm7f6kSEnjHKp0/4TSSOZgA==')).decode('utf8'))
\$\endgroup\$
2
  • 4
    \$\begingroup\$ You can get it down to 391 chars by doing something like: import zlib;print zlib.decompress("""eJzlkLFOxDAQRPt8xXTXoHwHdEhEot7L+myD8Z7snKz8Pd6LT7mgFFQIRGftPs/OzOBMMiiUQRES GIF4RnGCXCgEKYZBOIW5B7onsMTDhPcopTIzsjN33ORoUvShos8mOTpnJQ4hgL3pu2741rF89myS igwqWJK3NugmMu6eb+3tY648qrRafPniyDQ5TIKRptFdZ83kz+Q5+sQq8ViP0DFfEquZRrT9vnXd bI2v3fzCoPXs9dgHWR/NorpJWoH9oONCrr5vnf35TlisKryqGsGJ3TZS1/uN8EKurlu5/6k7Jymb m7f6kSEnjHKp0/4TSSOZgA==""".decode('base64')) \$\endgroup\$
    – ESultanik
    Commented Sep 21, 2011 at 15:40
  • \$\begingroup\$ Creative way to compress this. \$\endgroup\$
    – Joseph
    Commented Jun 25, 2017 at 23:31
3
\$\begingroup\$

JavaScript (422)

Works in the SpiderMonkey interpreter versions used by both anarchy golf and ideone.

for(a="fly0spider0bird0cat0dog0horse0How absurd0Imagine that0What a hog".split(i=0);p=print;p("I don't know why she swallowed that fly,\nPerhaps she'll die.\n")){p("There was an old lady who swallowed a",a[i]+(i?",":"."));i>4&&quit(p("She died of course."));i&&p(i>1?a[4+i]+" to swallow a "+a[i]+".":"That wriggled and iggled and jiggled inside her.");for(j=i++;j;)p("She swallowed the "+a[j]+" to catch the "+a[--j]+",")}

A bit more nicely formatted:

for (a = "fly0spider0bird0cat0dog0horse0How absurd0Imagine that0What a hog".split(i = 0);
p = print; p("I don't know why she swallowed that fly,\nPerhaps she'll die.\n")) {
    p("There was an old lady who swallowed a", a[i] + (i ? "," : "."));
    i > 4 && quit(p("She died of course."));
    i && p(i > 1
        ? a[4 + i] + " to swallow a " + a[i] + "."
        : "That wriggled and iggled and jiggled inside her."
    );
    for (j = i++; j;) p("She swallowed the " + a[j] + " to catch the " + a[--j] + ",");
}
\$\endgroup\$
1
  • \$\begingroup\$ Clever use of the splitting by 0 trick to initialize i to 0 in only 2 bytes. Have a +1 \$\endgroup\$
    – Cyoce
    Commented Jan 6, 2017 at 23:32
3
\$\begingroup\$

ink, 370 369 354 bytes

LIST B=fly,spider,bird,cat,dog,horse
VAR s=" swallow"
-(i)~B=B(i)
There was an old lady who{s}ed a {B}{.|,}
{|That wriggled and iggled and jiggled inside her|How absurd|Imagine that|What a hog|She died of course.->END}{|.| to{s} a {B}.}
-(k)~B--
{B:
She{s}ed the {B+1} to catch the {B},
->k
}I don't know why she{s}ed that fly,
Perhaps she'll die.
\ 
->i

Try it online!

Explanation

LIST B=fly,spider,bird,cat,dog,horse           // Ink's "LIST"s aren't much like lists - they're more like enums. Here we create a list type stating what animals exist, and make a set containing none of them.
VAR s=" swallow "                              // Pretty self-explanatory
-(i)~B=B(i)                                    // The i label keeps track of how many times we've visited it. We set B (the set) to be equal to the i-th item in B (the list type).
There was an old lady who{s}ed a {B}{.|,}      // Print the first line. {.|,} is a sequence - it's a period the first time it's encountered, and a comma every subsequent time.
                                               // Sequences can have more than one item in them, which is how we print the second line. When we get to the horse one, we terminate the program right away.
{|That wriggled and iggled and jiggled inside her|How absurd|Imagine that|What a hog|She died of course.->END}{|.|to{s} a {B}}.}
-(k)~B--                                       // Step back through the list type.
{B:                                            // A conditional - we execure the next two lines if B is non-empty.
She{s}ed the {B+1} to catch the {B},           // Print the "she swallowed the..." line
->k                                            // And repeat
}I don't know why she{s}ed that fly,           // Only reached when the conditional fails.
Perhaps she'll die.
\                                              // A backslash and a space to force an empty line.
->i                                            // Back to the top
\$\endgroup\$
3
\$\begingroup\$

C (gcc), 429 424 423 bytes

-5 bytes thanks to ceilingcat

c,*w[]={"384fly<1","I don't know why she5ed that fly<Perhaps she'll die.772","There was an old lady who5ed a ","She5ed the "," to catch the "," swallow"," to5 a ","\n","spider","bird","cat","iggled ",",7","3948<0"};U(char*s){for(;c=*s++;)c>47&c<65?U(w[c-48]):putchar(c);}f(){U("2fly.718<That wr;and ;and j;inside her.709<How absurd69.7=:<Imagine that6:.73:49<=dog<What a hog6dog.73dog4:<3:49<=horse<She died of course.7");}

Try it online!

\$\endgroup\$
0
2
\$\begingroup\$

Ruby, 436 characters

w=" swallowed "
f="There was an old lady who#{w}a "
c=" to swallow a %s"
b="dog","cat","bird","spider","fly"
s=nil,"That wriggled and iggled and jiggled inside her","How absurd"+c,"Imagine that"+c,"What a hog"+c
a=[]
5.times{k=g=b.pop;t,*s=s
puts f+k+(t ??,:?.),t&&[t%k+?.,a.map{|x|y,k=k,x;"She#{w}the #{y} to catch the #{k},"}],"I don't know why she#{w}that fly,","Perhaps she'll die.",""
a=[g]+a}
puts f+"horse,","She died of course."
\$\endgroup\$
2
\$\begingroup\$

Scala (706 619 599 550 chars)

val L=Seq("fly","spider","bird","cat","dog")
val M="\nThere was an old lady who %sed a %s,"
type S=String
def s(l:S,a:S="",b:S="")=l.format("swallow",a,b)
def p(l:S,a:S=""){println(s(l,a))}
var i=0
var r=List[S]()
L.map{a=>{p(M,a)
if(i>0){p(Seq("","That wriggled and iggled and jiggled inside her.","How absurd","Imagine that","What a hog")(i)+(if(i>1)" to %s a %s."else""),a)
r::=s("She %sed the %s to catch the %s,",L(i),L(i-1))
r.map(p(_))}
p("I don't know why she %sed that fly,\nPerhaps she'll die.")
i+=1}}
p(M,"horse")
p("She died of course.")

Using map instead of foreach allows to squeeze more chars... In codegolf, we don't care about performance, elegance (non-mutability) or logic...

\$\endgroup\$
2
\$\begingroup\$

PowerShell Core, 503 484 bytes

$i=" bird"
$k=" swallowed"
$g="She$k the"
$h=" to catch the"
$c="There was an old lady who$k a"
$e=" to swallow a"
$s=" spider"
"$c fly."
$c="`n$c"
($a="I don't know why she$k that fly,`nPerhaps she'll die.")
"$c$s,`nThat wriggled and iggled and jiggled inside her."
($d="$g$s$h fly,")
$a
"$c$i,`nHow absurd$e$i."
($j="$g$i$h$s,")
$d
$a
"$c cat,`nImagine that$e cat."
($b="$g cat$h$i,")
$j
$d
$a
"$c dog,`nWhat a hog$e dog.`n$g dog$h cat,"
$b
$j
$d
$a
"$c horse,`nShe died of course."

Try it online!

It was a fun one! :)

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 237 222 218 224 bytes

+6 bytes to fix a bug (In most verses the first line ends in a ,)

“¤†É¾£‡—Ä‹·î€“#©€“€Ç€¥€¤„Ï£î€Ð 1ed€… ÿ,“®¦¦¨.•Ω\3¯ÓõÔ)V₄H`«Å₆éÅΘΓåò•ć¡δ"ÿ to 1 a ÿ."Å\“€Š wr0€ƒ 0€ƒ j0‹â‚®.“š“‚Îœ·€‚…£.“ª0šø0δK®ü“‚Î 1ed€€ ÿ€„¬ª€€ ÿ,“¨ηíõšε“i¥Ú'tƒ€„Ý‚Î 1ed€Š¤†,“ª“™¡‚Î'll—Ç.
“ª}õª.ι˜.ª»T.•4Ƶ›₅8zεÚS•#‡¨','..;

Try it online!

Commented:

“¤†É¾£‡—Ä‹·î€“              # push dictionary compressed string "fly spider bird cat dog horse"
#                           # split on spaces
©                           # store the list of animals in the register
€“€Ç€¥€¤„Ï£î€Ð 1ed€… ÿ,“    # for each animal, push the string "there was an old lady who 1ed a ÿ,"
                            #   where ÿ s replaced by the animal
®                           # push the list of animals again
¦¦¨                         # remove the fly, the spider and the horse: ["bird", "cat", "dog"]
.•Ω\3¯ÓõÔ)V₄H`«Å₆éÅΘΓåò•    # push alphabet compressed string "xhow absurdximagine thatxwhat a hog"
ć                           # extract the first character "x"
¡                           # split the remaining string on "x": ["how absurd", "imagine that", "what a hog"]
δ"ÿ to 1 a ÿ."Å\            # format the string "ÿ to 1 a ÿ." with a value from this list and one from the short animal list
                            #  => ["how absurd to 1 a bird.", "imagine that to 1 a cat.", "what a hog to 1 a dog."]
“€Š wr0€ƒ 0€ƒ j0‹â‚®.“      # push compressed dictionary string "that wr0 and 0 and j0 inside her."
š                           # prepend this to the list
“‚Îœ·€‚…£.“                 # push compressed dictionary string "she died of course."
ª                           # append this to the list
0š                          # prepeand a 0
ø                           # zip both lists together
0δK                         # remove 0's in sublists
                            #  => [["there was an old lady who 1ed a fly,"],
                            #      ["there was an old lady who 1ed a spider,", "that wr0 and 0 and j0 inside her."],
                            #      ["there was an old lady who 1ed a bird,", "how absurd to 1 a bird."],
                            #      ["there was an old lady who 1ed a cat,", "imagine that to 1 a cat."],
                            #      ["there was an old lady who 1ed a dog,", "what a hog to 1 a dog."],
                            #      ["there was an old lady who 1ed a horse,", "she died of course."]]
®                           # push the list of animals again
ü“‚Î 1ed€€ ÿ€„¬ª€€ ÿ,“      # format the string "she 1ed the ÿ to catch the ÿ," with adjacent pairs of animals
¨                           # remove the last string
                            #  => ["she 1ed the spider to catch the fly,", "she 1ed the bird to catch the spider,", "she 1ed the cat to catch the bird,", "she 1ed the dog to catch the cat,"]
η                           # take the prefixes of this list
í                           # reverse each prefix
õš                          # prepend the empty string
ε                           # map over the prefixes:
  “i¥Ú'tƒ€„Ý‚Î 1ed€Š¤†,“    #   push compressed string "i don't know why she 1ed that fly,"
  ª                         #   append to the prefix
  “™¡‚Î'll—Ç.\n“            #   push compressed string "perhaps she'll die.\n"
  ª                         #   append to the prefix
}                           # end of map
õª                          # append an empty string to the list of prefixes
.ι                          # interleave the list of prefixes with the earlier list
˜                           # flatten everything into a list of lines
.ª                          # sentence-case each line
»                           # join by newlines
T                           # push 10
.•4Ƶ›₅8zεÚS•                # push compressed string "swallow iggled"
#                           # split on the space
‡                           # in the poem replace every digit of 10 with the corresponding word in this list
                            #   1 -> swallow, 0 -> iggled
¨                           # remove a trailing newline
','..;                      # replace the first "," with a "."
\$\endgroup\$
2
\$\begingroup\$

Stax, 195 bytes

ó┤♣≤jß¡εD<Çjò≡²£c¼ìv┴Åô║,·~┼¢╝EΦ5xZ&√σ"ømÿ≡ZπvÅ╦♣1♦>▄\¬◘¡jS¢í╞╟╚);4j↕i¢s♦éÇöy‼ac┴àÇ╩»-ó»ónú¢│g5εÿ╔µ┬bîæ¢k‼f╓fJ○▐═╜<^Γ▀o▀Δnq░╩αÄ&│♣ⁿâmû╣Ç≡*@☺pG_ⁿö┤mσ/V\└?iq┌îB½Ä¿L╙N┐£Ç≥δ2 0δrµLä⌡(⌡▀╬àΦ⌠tƒìg" ⌠ZHó

Run and debug it

Unpacked, ungolfed, and commented, it looks like this.

`]q~*}8x9uA3_P0O`jX         push ["fly", "spider", "bird", "cat", "dog"] and store in X register
{                           start iteration over animal list
  `p{+P91{m@Lwe\Jk?!#8`Yp   Store "There was an old lady who swallowed a " in Y register and print it
  .,.i!@+                   Concatenate "." on the first iteration and "," thereafter to the animal
  P                         Print
  i{                        On all iterations after the first, execute this block
    `r4uB2O'O(4XghNqE PhaMg^-riMgR{N>A8LfCNqh@Rn$=aHX|93@_5d?d7qp2-`
                        Push "That wriggled and iggled and jiggled inside her. How absurd, bird. Imagine that, cat. What a hog, dog. She died of course"
    ',`?~nA${F"`R       Replace "," with " to swallow a"
    .. /                Split on string ". "
    iv@                 Get the (i-1)th element where i is the iteration index
    '.+P                Concatenate "." and print
  }M                    End of conditional block
  x2B                   Get adjacent pairs from x (list of animals)
  i(                    Keep first i pairs where i is the iteration index
  r{                    Iterate over reversed list of pairs
    `_,m+"S3&#`p        Print "She swallowed the "
    Ep                  Print the second animal from the pair
    `p<qr?t`p           Print " to catch the "
    ',+P                Concatenate "," to the first animal from the pair and print
  F                     End iterating block
  `;p(9Cv,77BzpP'kB|"`P Print "I don't know why she swallowed that fly,"
  `%?/3.=iHiS6y!`P      Print "Perhaps she'll die."
  zP                    Print a newline
F                       End outer animal iteration.
yp                      Print y register. ("There was an old lady who swallowed a ")
`zvVH`P                 Print "horse,"
                        Print "She died of course." with an unterminated literal
`AJt6Q.3+$!

Run this one

\$\endgroup\$
2
\$\begingroup\$

Zsh + coreutils, 406 375 bytes

g()ls -t<>"She$d the $1 to catch the $2,"
set bird spider \ swallow iggled\ 
<<Z
${b=There was an old lady who${d=$3ed} a }fly.${5=
I don't know why she$d that fly,
Perhaps she'll die.

$b}$2,
That wr$4and $4and j$4inside her.
`g $2 fly`$5$1,
How absurd${6= to$3 a }$1.
`g $@`$5cat,
Imagine that$6cat.
`g cat $1`$5dog,
What a hog$6dog.
`g dog cat`$5horse,
She died of course.

Try it online!

Tricks:

  • the g function creates the line as a file and then ls lists the files in the current directory in order of creation (-t), to avoid having to repeat the previous lines every time.
    • I used <> (which creates the file before the command is executed) instead of >...; because using multiple commands in a function requires {} around them
  • set bird ... assigns the words to $1, $2, etc.
  • \ swallow includes a space at the start of without needing to use quotes
  • <<Z starts a heredoc which is effectively a long interpolated string
  • ${b=There...} is a shorthand form setting b to that string and substituting its value in
  • Indexed variables like $1, $2, etc. can be substituted without a space afterwards (rather than being interpreted as a variable called 5dog, for example), so we prefer them as it allows us to include the space inside the assignment to avoid repeating it
  • g $@: since we set $1 and $2 to bird and spider respectively, we can pass all the numbered variables to g in one go with $@ instead of g $1 $2 (it ignores the extra ones, $3 etc.)
\$\endgroup\$
1
\$\begingroup\$

Haskell, 515 498

Score does not count newlines and spaces added for presentation.

b c a="There was an old lady who swallowed a "++a++c++".\n"
t r a=b(",\n"++r++" to swallow a "++a)a
s(a,b)="She swallowed the "++a++" to catch the "++b++",\n"
z=["fly","spider","bird","cat","dog"]
h=[b"",b",\nThat wriggled and iggled and jiggled inside her",t"How absurd",
 t"Imagine that",t"What a hog"]
v(g,p)=g++(=<<)s(zip p$tail p)++
 "I don't know why she swallowed that fly,\nPerhaps she'll die.\n\n"
main=putStr$(=<<)v(zip(zipWith($)h z)(tail$scanl(flip(:))[]z))++b
 ",\nShe died of course""horse"

Ungolfed:

type Animal = String
type Comment = String

beginning :: Animal -> Comment -> String
beginning animal comment = "There was an old lady who swallowed a " ++ animal ++ comment ++ ".\n"

ending :: String
ending = "I don't know why she swallowed that fly,\nPerhaps she'll die.\n\n"

to_swallow :: String -> Animal -> Comment
to_swallow start animal = ",\n" ++ start ++ " to swallow a " ++ animal

swallowed_to_catch :: (Animal, Animal) -> String
swallowed_to_catch (a, b) = "She swallowed the " ++ a ++ " to catch the " ++ b ++ ",\n"

animals :: [(Animal, Animal -> Comment)]
animals = [("fly",    const "")
          ,("spider", const ",\nThat wriggled and iggled and jiggled inside her")
          ,("bird",   to_swallow "How absurd")
          ,("cat",    to_swallow "Imagine that")
          ,("dog",    to_swallow "What a hog")
          ]

-- Turn [1,2,3,4,5] into [[1], [2,1], [3,2,1], [4,3,2,1], [5,4,3,2,1]]
trail :: [a] -> [[a]]
trail = tail . scanl (flip (:)) []

verses :: [String]
verses = zipWith verse animals (trail $ map fst animals)

verse :: (Animal, Animal -> Comment) -> [Animal] -> String
verse (animal, comment) swallow_chain =
    beginning animal (comment animal) ++
    concatMap swallowed_to_catch (zip swallow_chain (tail swallow_chain)) ++
    ending

poem :: String
poem = concat verses ++ beginning "horse" ",\nShe died of course"

main :: IO ()
main = putStr poem
\$\endgroup\$
2
  • \$\begingroup\$ Aliasing (++) with a shorter identifier might be a good idea. \$\endgroup\$ Commented Sep 19, 2011 at 20:05
  • \$\begingroup\$ @ThomasEding or aliasing it to the # operator or something similar \$\endgroup\$
    – Cyoce
    Commented Jan 6, 2017 at 23:34
1
\$\begingroup\$

Python, 484

Ok, I did it but was pretty boring...

The last sentence is always with "fly" so some chars were removed...

f,s,b,c,d=o='fly spider bird cat dog'.split()
x='There was an old lady who swallowed a %s.\n'
t=' to swallow a %s.\n'
r=['That wriggled and iggled and jiggled inside her.\n','How absurd'+t%b,'Imagine that'+t%c,'What a hog'+t%d]
t=("She swallowed the %s to catch the %s,\n"*4%(d,c,c,b,b,s,s,f)).split('\n')
print("I don't know why she swallowed that fly,\nPerhaps she'll die.\n\n".join([x%f]+[x%o[i]+r[i-1]+'\n'.join(t[-i-1:])for i in range(1,5)]+[''])+x%'horse'+'She died of course.')

Less golfed version:

f,s,b,c,d = all = 'fly spider bird cat dog'.split()

what = 'There was an old lady who swallowed a %s.\n'

t = ' to swallow a %s.\n'
comments = ['That wriggled and iggled and jiggled inside her.\n',
            'How absurd' + t%b,
            'Imagine that' + t%c,
            'What a hog' + t%d]

swallowed = "She swallowed the %s to catch the %s,\n"
lines = (swallowed*4%(d,c,c,b,b,s,s,f)).split('\n')

end = '''I don't know why she swallowed that fly,
Perhaps she'll die.\n\n'''

def who_catch_who(i):
    return '\n'.join(lines[-i-1:])

p = end.join([what % f] + 
             [what % all[i] +
              comments[i-1] +
              who_catch_who(i) for i in range(1,5)] +
             [''])

print(p + what % 'horse' + 'She died of course.')
\$\endgroup\$
1
\$\begingroup\$

C, for fun (561 chars)


Score does not count newlines and spaces added for presentation.

Thanks to J B for his improvements!

#include <stdio.h>

void main() {
    char *e = "\nShe swallowed the cat to catch the bird,\nShe swallowed the bird to catch the spider,\nShe swallowed the spider to catch the fly,\nI don't know why she swallowed that fly,\nPerhaps she'll die.\n\nThere was an old lady who swallowed a ";
    printf("%sfly.%sspider,\nThat wriggled and iggled and jiggled inside her.%sbird,\nHow absurd to swallow a bird.%scat,\nImagine that to swallow a cat.%sdog,\nWhat a hog to swallow a dog.\nShe swallowed the dog to catch the cat,%shorse,\nShe died of course.", e+191, e+128, e+85, e+41, e, e);
}
\$\endgroup\$
15
  • \$\begingroup\$ Are you sure you need all those & and []? \$\endgroup\$
    – J B
    Commented Sep 20, 2011 at 7:23
  • 1
    \$\begingroup\$ @J B is right, you can replace a[] with *a and just remove the &. You should gain 25 chars. \$\endgroup\$
    – PhiLho
    Commented Sep 20, 2011 at 12:38
  • \$\begingroup\$ There's probably something cool to be done in C, with pointers to the middle of a common shared string. \$\endgroup\$
    – J B
    Commented Sep 20, 2011 at 13:37
  • 1
    \$\begingroup\$ Something like this. Your version, reordered, 627 chars pastebin.com/MQ4LJue7 ; my version, 561 chars pastebin.com/7eMAWmAi \$\endgroup\$
    – J B
    Commented Sep 20, 2011 at 18:16
  • 1
    \$\begingroup\$ Technically new lines and spaces should be removed from answers, if the score does not count them. codegolf.meta.stackexchange.com/questions/6886/… (I was informed of this myself recently). But you can post the ungolfed version below it. \$\endgroup\$ Commented Jun 26, 2017 at 8:34
1
\$\begingroup\$

C#, 556 chars

class P{static void Main(){string a="There was an old lady who swallowed a ",b="I don't know why she swallowed that ",c="She swallowed the ",d=" to catch the ",e="Perhaps she'll die.\n\n",f="fly",g="spider",h="bird",i="cat",j="dog",k=",\n",l=".\n",m="to swallow a ",n=c+g+d+f+k,o=c+h+d+g+k,p=b+f+k,q=c+i+d+h+k,r=n+p,s=o+r,t=q+s,u=e+a;System.Console.Write(a+f+l+p+u+g+k+"That wriggled and iggled and jiggled inside her"+l+r+u+h+k+"How absurd "+m+h+l+s+u+i+k+"Imagine that "+m+i+l+t+u+j+k+"What a hog "+m+j+l+c+j+d+i+k+t+u+"horse"+k+"She died of course.");}}
\$\endgroup\$
1
\$\begingroup\$

Perl, 489 chars

sub Y{$t=pop;my$s;while($b=pop){$s.="She ${W}ed the $t to catch the $b,\n";$t=$b;}$s.=$l{fly}.$/;}$W='swallow';$w='iggled';$q=" to $W a";$o="There was an old lady who ${W}ed a";$h='hat';@f=qw/fly spider bird cat dog/;@l{@f}=("I don't know why she ${W}ed t$h fly,
Perhaps she'll die.$/","T$h wr$w and $w and j$w inside her.","How absurd$q bird.","Imagine t$h$q cat.","W$h a hog$q dog.");map{$m=$f[$_];print"$o $m,
$l{$m}
",$_?Y@f[0..$_]:'';}0..$#f;print"$o horse,
She died of course.$/"
\$\endgroup\$
1
\$\begingroup\$

PHP, 344 bytes

<?=gzinflate(base64_decode("5VAxTsQwEOz9iu2uifIO6JA4iXovu2cbjPdk+2Tl96zjoLugFFQIRGfNjGdn5ug4MVTMgBEkEASkGaoTyBVDkMoECOcwj+YRSOKhwFuUqooZsuM7VXFYmnAwT5wcXnLjDyEAeR6NOX7rUL544jSoWs1q8taGhkeCu+fr+vYxqxrUeDTPX7LwagVFYMIyuQVb4v1Ej5NPNJgHPYCnfE3UYqz8yu5Fbvg28Ocev6yentRD72h95O646af0Xr2p6255+0p/eAcSO5iX5oTgxG5XUHJvBeq6W9plzP+wlpOUuafSbwRyhkmuio0f"));

Try it online!

PHP, 405 bytes

<?=strtr("0fly.
106That wr8and 8and j8inside her.
23107,
How absurd57.
274623109,
Imagine that59.
2947,
27462310dog,
What a hog5dog.
2dog49,
2947,
27462310horse,
She died of course.",["There was an old lady who swallowed a ","I don't know why she swallowed that fly,
Perhaps she'll die.

","She swallowed the ","spider to catch the fly,
"," to catch the "," to swallow a ","spider,
",bird,"iggled ",cat]);

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Bubblegum, 255 bytes

0000000: e0 04 85 00   f7 5d 00 2a   1a 08 a7 55   8e 22 31 d2 │ à...÷].*..§U."1Ò
0000010: cb f7 30 fa   52 4d 88 81   61 e4 be 9d   c2 f0 0e f8 │ Ë÷0úRM..aä¾.Âð.ø
0000020: dc c9 de 24   63 c9 1f c7   f7 71 e4 ed   40 68 0e d2 │ ÜÉÞ$cÉ.Ç÷qäí@h.Ò
0000030: cd 76 d8 d9   0d 61 78 f7   40 cf 23 95   48 e9 be 27 │ ÍvØÙ.ax÷@Ï#.Hé¾'
0000040: aa 75 57 ff   51 9e 1f 1f   25 d8 93 4d   18 69 c9 01 │ ªuWÿQ...%Ø.M.iÉ.
0000050: 12 16 ec c1   ff e0 01 7e   fa ea 1e cc   84 d7 58 b8 │ ..ìÁÿà.~úê.Ì.×X¸
0000060: 47 d4 40 b4   ff c7 64 a9   2f 07 bf 7b   f4 25 74 94 │ GÔ@´ÿÇd©/.¿{ô%t.
0000070: af da 8a fc   0c 18 81 58   b8 3c 2e 97   c0 9d e8 27 │ ¯Ú.ü...X¸<..À.è'
0000080: 3e 02 8a d2   1b 7c 94 cc   cb f4 05 7c   c7 77 f3 75 │ >..Ò.|.ÌËô.|Çwóu
0000090: ea 7e 02 d6   3a 84 5c 4e   1f 88 e7 03   9a 1d 3f 13 │ ê~.Ö:.\N..ç...?.
00000a0: a6 9f 2e cf   be 77 16 be   f1 5f 01 52   cf 13 89 b3 │ ¦..Ͼw.¾ñ_.RÏ..³
00000b0: f3 8a f7 90   18 08 50 99   27 f7 83 d2   a4 32 08 76 │ ó.÷...P.'÷.Ò¤2.v
00000c0: ef b4 99 6c   80 dd 1a 47   04 26 fe b1   02 b3 d4 e7 │ ï´.l.Ý.G.&þ±.³Ôç
00000d0: 3d 44 3a 64   64 46 39 77   35 61 6b 6c   7b 68 34 db │ =D:ddF9w5akl{h4Û
00000e0: 51 75 39 2e   bc 46 8e 96   d1 8a 4c 79   f4 7a 7b e0 │ Qu9.¼F..Ñ.Lyôz{à
00000f0: f5 44 85 eb   ef 68 d5 22   26 4a 2a ef   fc 60 00    │ õD.ëïhÕ"&J*ïü`.
\$\endgroup\$
1
\$\begingroup\$

Python 2, 453 442 427 387 bytes

  • @nore saved 11 bytes: Thanks a ton!!: pointed £ used before was a double byte char!!
  • saved 15 bytes: swallow shorthand
  • @nore saved 40 bytes!!!!! THANKS A LOT!!!
  • @Zachary T saved 1 byte: space between in and "..." removed
  • Thanks to notepad++'s find and replace tool. ;)
r="%2(:0)That wr66j5 inside her(_1)How absurd+1({3)Imagine that+3(}4)What a hog+4(~4*3)}horse)She died of course."
for i in"}~3*1){!{~1*0)_!_~0*2):!:^2)$%!~She7ed the !%There was an old lady who7ed a !$Perhaps she'll die(\n!* to 3ch the !^I don't know why she7ed that !+ to7 a !(.\n!),\n!0spider!1bird!2fly!3cat!4dog!65 and !5iggled!7 swallow".split("!"):r=r.replace(i[0],i[1:])
print r

Try it online!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ You can save a lot of bytes by using ~ instead of £ (which is two bytes long as it is not ASCII). \$\endgroup\$
    – nore
    Commented Jun 25, 2017 at 16:38
  • \$\begingroup\$ Woah, Thanks man! Never knew that was an extended chararcter!! \$\endgroup\$
    – 0xffcourse
    Commented Jun 25, 2017 at 16:42
  • 1
    \$\begingroup\$ This can still be pretty heavily golfed (a few chars with the symbols at the beginning, 1 by using Python2, and more optimizing replacements) \$\endgroup\$
    – nore
    Commented Jun 25, 2017 at 18:23
  • 1
    \$\begingroup\$ You don't need a space between in and a string literal. \$\endgroup\$
    – Adalynn
    Commented Jun 25, 2017 at 23:30
1
\$\begingroup\$

Groovy, 475 bytes

b="swallowed"
a="There was an old lady who $b a"
c="I don't know why she $b that fly,\nperhaps she'll die."
d="She $b the"
e=" to catch the"
f=" to swallow a"
g="iggled"
h=" spider"
i=" fly"
j=" dog"
k=" cat"
l=" bird"
m="$d$h$e$i,"
n="$d$l$e$h,"
o="$d$k$e$l,"
print"""
$a$i.
$c

$a$h,
That wr$g and $g and j$g inside her.
$m
$c

$a$l,
How absurd$f$l.
$n
$m
$c

$a$k,
Imagine that$f$k.
$o
$n
$m
$c

$a$j,
What a hog$f$j
$d$j$e$k,
$o
$n
$m
$c

$a horse,
She died of course."""

Nothing too interesting, just a lot of string interpolation. Golfing tips on this one are welcome!

\$\endgroup\$
2
  • \$\begingroup\$ did you mean to write swallowed, not shallowed? \$\endgroup\$ Commented Jun 25, 2017 at 23:51
  • \$\begingroup\$ @Destructible Lemon it is fixed now :P \$\endgroup\$ Commented Jun 26, 2017 at 6:00
1
\$\begingroup\$

tcl, 451 bytes

lassign {\ swallowed fly, spider " to catch the " bird cat iggled " to swallow a"} W f s c b a i w
puts "[set t "There was an old lady who$W a "]fly.
[set I "I don't know why she$W that $f
Perhaps she'll die.

"]$t$s,
That wr$i and $i and j$i inside her.
[set S "She$W the "]$s$c$f
$I$t$b,
How absurd$w $b.[set v \n$S$b$c$s,\n$S$s$c$f\n$I]$t$a,
Imagine that$w $a.
[set X $S$a$c$b,$v$t]dog,
What a hog$w dog.
${S}dog$c$a,
$X\horse,
She died of course."

Available to run on: http://rextester.com/live/GXF89639 (10th attempt)

\$\endgroup\$
1
\$\begingroup\$

PowerShell, 349 bytes = Script:9 + Archive:340

tar xOf t

Try it online!

The Powershell script to create the archive t (see TIO):

(
@'
There was an old lady who swallowed a fly.
I don't know why she swallowed that fly,
Perhaps she'll die.
......
There was an old lady who swallowed a horse,
She died of course.
'@
) | Set-Content f -Force
tar zcfo t f
Get-ChildItem f,t # output info about archive size
\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 295 bytes

BinaryDeserialize@ByteArray@ToCharacterCode@"8C:xœåPANÃ0<ãWì­—(ï€R‘8o³[ÛÅx+;••ðnÖqª6(NÄÍšÏÎÌþãáÅqb(˜#H H'† …	Žaê͐ÄÝoQŠ*&ÈŽïT£Ã±
;óÌÉá9W~çÞ˜ïÊgOœ:U«YIÞÚPñHp÷<-o³ªA{³ÿ’…+7cs¼Ÿèqð‰:ó¨ð/‰jŒ…_Ø­È_¾îñËêéI=ôŽÖGnŽ«~JoÕšî–·­ô‡w ±y­NNìz%·V ¦»¥Çük9I™[*ýF Gä¢Xÿ	Tÿ”\""

Try it online!

This solution is a 46-byte decompressor BinaryDeserialize@ByteArray@ToCharacterCode@"..." acting on a 248-byte array masquerading as an ASCII string. Transferring this string between different programs is a bit tricky. TIO, for instance, blows this solution up into 416 bytes using UTF-8 encoding instead of keeping the ASCII string. To get the minimal 295-byte solution, save the text in a variable text and run

BinaryWrite["ThereWasAnOldLady.wl", Join[
  ToCharacterCode["BinaryDeserialize@ByteArray@ToCharacterCode@\""],
  Normal[BinarySerialize[text, PerformanceGoal -> "Size"]] /. {34 -> Sequence[92, 34]},
  ToCharacterCode["\""]]]

then execute the generated file on the command-line with

wolframscript -print -f ThereWasAnOldLady.wl 
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 512 493 bytes

-19 bytes thanks to ceilingcat

*s[]={"That wriggled and iggled and jiggled inside her.","How absurd to swallow a bird.","Imagine that to swallow a cat.","What a hog to swallow a dog.","fly","spider","bird","cat","dog","horse"};r(i,x){return i&&r(i-1,!printf("She swallowed the %s to catch the %s,\n",s[i+4],s[i+3],x&&puts(s[i-1])));}f(i){for(i=0;printf("There was an old lady who swallowed a %s.\n",s[i+4]),i<5&&puts("I don't know why she swallowed that fly,\nPerhaps she'll die.\n",r(i++,1)););puts("She died of course.");}

Try it online!

\$\endgroup\$
6
  • \$\begingroup\$ Since the program is kolmogorov complexity, that implies no input, so input is not allowed in your answer to the question. \$\endgroup\$
    – Gio D
    Commented Jan 14, 2021 at 2:53
  • \$\begingroup\$ Oh all right, thank you for letting me know. I am going to delete the answer \$\endgroup\$
    – anotherOne
    Commented Jan 14, 2021 at 2:56
  • \$\begingroup\$ @GioD I made a new program :D \$\endgroup\$
    – anotherOne
    Commented Jan 14, 2021 at 16:03
  • \$\begingroup\$ You've forgotten the horse. \$\endgroup\$ Commented Jan 14, 2021 at 16:47
  • \$\begingroup\$ I added the horse! \$\endgroup\$
    – anotherOne
    Commented Jan 14, 2021 at 22:48
1
\$\begingroup\$

05AB1E, 516 408 chars

-108 chars by not restricting the allowed characters to CJK Unified Ideographs Extension B

"󍫜񐶆򅏠򞱢񾇜򂺸󰨑򉶜󋊕𡝈𲺾񒝧򝲉􌱯􈙽󗘵󆈪󩙳򏐗񠲿󘅹򙴲򟝎򏈮𫛮򼶲󛉼񚛼񩝆񦈿󡶈򧊄򿢭񼵂񴣰𑴦񹿳󐣇񂝏󒅧􃜁򖻌򏷾󠉵󕽇𠃗󏢹𗿄𐽢󒈏򐄏򂿟򎠿󌳏񓬰􌎇򗿀􇜠񙏼򚰬􂒒󘡽􈲂񸭈򖍨򄁙򊥈񹈧򀇔򬟪𕃝󯘒𮨉򵛙󟩶򝂏񉢼񒿾𻜒􈏸󜱿򨙨🌨􀡽𜴂񷟠𼸯򞺯򪰩򇿙􌱊򖀸󛩯𐸝񕦀𗌬󱩲򩹶񦈄񀧐𿂉󰤧񱞩󇈶🝫􎟃𼧕񹽖򥤭󅥚𭛨񯖅𥵰󗁄񜤈񛩾򈺼񩒉𧓧񳡠𚨅𬮾􋡈񭂭󧚝󬦀򽞲󳷑򕚒󲫬󜣡󌇙񀿺𗨻񅎱􈙆򌣳󯑀󯩌󩄀󴾏򂃻󺝼򀙣󫴋񶏩󦭅򹶮󹝭򘣵򾾲򀏠𰮄𝜟𴄔𤉸𳰠򦳫𑇈󯃝񮪷񐼇󐔏󿠲𐆬󎰖򅁕񝾗𦴃󸫣򇋿񦉬򱒎󗘡󶆣𥪁󝖌󪬂𭡎󄎕󝣘􀕹񘎓𚢗󶧴񄩓􂪎񑦳򬪳𯨯󛩃𖚞󕫯񗭿񋧉􀥥񙩤𝑸󇱌􅧹򰖀󏃋򞶘򚓦󌶙𑌨񳦴򼫰򟻖򂪷𙄢򫓎񄞛󋹆󾜧􊻞󄑀񛘁򁀶𥨪򶓣𓌃񛑕񟲧󑤣򊠯򆹠򷠶󜚹􀷦򥼴񻹚𝰧𩢀񐚌𑨮𙎢񟏊󶯡󕄂𲚆󶖉󦀤򔽐򹳜󬔨􄡡񁶏𖞑򄳟𦭊񆖜񗺄񇣢񹓨񩽅񳌾򳙙񽁻𗲵󎘺𠽎򀖻󌇙𝗀񶧴󷱳򩱄𢠊򼒐𶌥𞰪𪉰񟆯򍐳𕟄񴴕򚘙󭮛󳝼񼣮񉸅򼺴𐻙򗊎񫐪򰊦󻭸𙡤𧬚𕚾􍐠󢊬񂑽򟬁𶋏񮘎󾙺"ÇŽáá-•Gαm•β₆BŽm&",.' 
"‡"𝹇򝢇󰬾󹭟򪄙񗜂򗡅𞦂򽇎􌡱󼳥򼵉𰋈򨗄𥅌󫠝󔌓󄯇򑍘󏳊𶃀򱼺𛒴񥻳򲉌񣵆󾱧򼣹𾱫𠃀󶸦񝗷󐷮󜳄𺺅𻲆󳈆󅱃򞠩񴸹󬮽򫯌򰶢񝉫񆥦󟾨򗕼𞅀򘂨􁌣𧤀򚂅񒎠􃷆󖱎𘼨𬿊󙵲񏭉񼀀"ÇŽáá-•Gαm•βbT„Aa‡.Ï

Try it online!

This exploits the fact that this is an old question that counts in chars.

Version without using UTF-16 strings, 925 bytes

•*™w8?áZм<r¼Ë}ÈdEÄŸ€Kβ;’Ì÷`₆NËr;OÙнΛ¤øoE~ÝùÇ?}6űT÷Eā2²ïápK|¯Ð†˜CYîâÀJ¯Ð₅“λ˜'=\₃0ˆN‹BM©мe½Ì½ÜÙÈ?QXt”
v´qØú!þ3$òšpô/—èi_ð@tγµðB}¯S₄6´>ÝN¯ζlç懼¶§āj]\0þGwN₁<âX1oƵ[Òg³KÓ‹ì‚üÿ₃1E®Ìηʒ(ΔÃλËXŒlµV#₃ Τݴö÷ε_%C+zÝ-C-†¬‘mö¹z∍³ǝq†€Tʒ„2žUÞ>Ï©ι‹ó≠ÿöì₆н—₃\мā9þ6jΩÛγ„€LÙоYm9îîaü±µŒžÖ»Ÿæ×çîóú®ùŽÊ>Ô₄G÷5Λ‚sÙ₆=øHlãsPÏØ\¬»E†¯ÄÇŠP@¡ŠÁ*Ñ₁oƵ°eø¾Ù₂Îæ₂.Â(₅”uCDr‡₃OþR₃Æ-&ÑâÀ₄˜g¾þ;q‡8}5,z—H÷‹}Þjô&„HKFÈ≠ΘúĆ0¸aJ+bàGÈzÞÉY4w-/ʇα_Ù$!i±—þq9ǝ*SнǝθeBQÑK+}‰Ý‰—‡Ω¶Σx¢32DÓ†vª‰Í%иt5ž*|Îæ)³₄¡NýXн¦®ÇÃþ¸.¸ò8r¢›§e›Ì"P₆ƵāÑÖºjΣ~ï`
∞åŒwïìsк∊ā‰Š®#¸вP=jû∊ÕÇëäûм—sJTǝ—ƒв¼êι‘Ù†¨ΓõćIÎ嚢4û6é›a™ÌV½ƶ≠ÃÑlëð≫˜p:™°½“°2ï$£äζgŸ
¯ΛÿĆ„…û≠Q³¹îèïuøÉ₁¦ÁÝl^¨{Ê}мÁñ₄<нΘï÷@ãr1ôÑÌнÕ´…+ÕûÒO}öNηÆßã‘ó÷ß0xå1_|£ÒζÜqzÚ¼W^ÕæÓt\[šIvε©lø‚Õ∊H∞OŒƵŒ›ö;»E¾ƶćĀ#8lràQ˜aç½>¬λÓ™¾²ópöη¼Ā-æĆ„Wp6m”^tøgOõΛ¦≠ÅúÑDβ=ʒRJζã₂4N,]ÄKåí•₆BŽm&",.' 
"‡•vŽ²~³ÖĀw`]¬ê₂R∞v³ÿkØæ!Xh¤li£4ĆdCµ Aëp
UΑ±6ʒιéµÞ3Y>Ý{#r9›½, å<š*å$”Wµ->Mé₅åˆíÏV"£³§`%kIŽ²,‡èᲂ… ÙP§¢∞>r”∍(+Ā~∊³β¨ÔX3Ãðسž˜ûтÇiúÉ,9ΩÍÌJöεš¢≠«¼“UPºi]¤z¥•bT„Aa‡.Ï

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ I believe most Chinese people may know 0 of these Han characters on average. @_@ \$\endgroup\$
    – tsh
    Commented Jan 14, 2021 at 3:25
  • \$\begingroup\$ Is there reason you choose such a "low" base for your first anwer? Unicode character codes go up to 1114112. \$\endgroup\$
    – ovs
    Commented Jan 14, 2021 at 7:28
  • \$\begingroup\$ @ovs 42719 is the number of defined characters in the largest Unicode block (CJK Unified Ideographs Extension B). For some reason, I did not want to use undefined characters. \$\endgroup\$
    – PkmnQ
    Commented Jan 14, 2021 at 7:42
0
\$\begingroup\$

Java 758 chars

Here is my Java effort ( 758 chars )

public class Poem{public static void main(String[] args){String s1="swallowed",st="There was an old lady who "+s1+" a |.",d1="Perhaps she'll die.",d2="She died ofcourse.",e="I don't know why she " + s1 + " that fly,\n";String[] a={ "fly","spider","bird","cat","dog","horse"};String[] q={ "","That wriggled and iggled and jiggled inside her.","How absurd |","Imagine that |","What a hog |",null};for(int i=0;i < a.length;i++){print(st.replace("|",a[i]));if(q[i]==null){print(d2);break;}if(!q[i].isEmpty()){               print((q[i].replace("|","to swallow a " + a[i])).trim());for(int j=i;j > 0;j--){print("She swallowed the " + a[j] + " to catch the " + a[j - 1] + ",");}}print(e + d1 + "\n");}}
static void print(String value){System.out.println(value);}}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ You can gain some chars... Poem => P, args => x, print => p, value => v, two letters vars => one letter vars, removing more spaces. \$\endgroup\$
    – PhiLho
    Commented Sep 21, 2011 at 15:47
0
\$\begingroup\$

Java, 655 bytes

public class M{public static void main(String[]P){String S="swallowed",T="There was an old lady who "+S+" a |.",e="I don't know why she "+S+" that fly,\n";String[]a={"fly","spider","bird","cat","dog","horse"};String[]q={"","That wriggled and iggled and jiggled inside her.","How absurd |","Imagine that |","What a hog |",null};for(int i=0;i<a.length;i++){p(T.replace("|",a[i]));if(q[i]==null){p("She died of course.");break;}if(!q[i].isEmpty()){p((q[i].replace("|","to swallow a "+a[i])).trim());for(int j=i;j>0;j--){p("She swallowed the "+a[j]+" to catch the "+a[j-1]+",");}}p(e+"Perhaps she'll die.\n");}}static void p(String v){System.out.println(v);}}

This is a golfed version of this answer. There's over 100 bytes saved on minor things.

\$\endgroup\$
2
  • \$\begingroup\$ Some more things to golf: public can be removed; class M{public static void main can be interface M{static void main; String S=...,T=...,e=...;String[]a=...;String[]q=...; can be String S=...,T=...,e=...,a[]=...,q[]=...;; for(int i=0;...)...for(int j=i can be for(int i=0,j;...)...for(j=i; for(j=i;j>0;j--)...a[j]...a[j-1] can be for(j=i;j>0;)...a[j]...[a--j]; static void p(String v){...} can be static<T>void p(T v){...} (623 bytes) Try it online. \$\endgroup\$ Commented Jan 29, 2018 at 12:05
  • 1
    \$\begingroup\$ 611 bytes building on @KevinCruijssen \$\endgroup\$
    – ceilingcat
    Commented Oct 31, 2021 at 20:56
0
\$\begingroup\$

Charcoal, 369 366 bytes

A”↓0*)B⎚D³9;7Q7∨-f¶_²χ⁶;↧l⁺I´”¦αA“V×q▶²▷O≕⦃0j↖▷(τ⎇⦄4ω\`j0T⁹ψI2b1züρÀ↑‹⁻ê£AZ‹?DOR§1φ”¦βA⟦spider¦bird¦cat¦dog⟧¦λA,¶¦νA⟦”↶7⎇IPVpθθ⪪➙S²p⸿H⁸✂ν‖2↖²φ↘nτME⦄B↓ξlF⁵y”¦”↶7Nl@;⪫λ38N∕´≕⁺)³ nÀ‴⦃”¦”↶7←´<7YKBYχnZ-↖U⎇ι\HUθ%;⟧”¦”↶7∧§‴⟧O×φ¢\`*NQ¶G⁺$π‽ξat”⟧μA”↓/!Zm⪫⟧αÀ⁻Vε>⟲”¦σA”|‽aC-⁴t;Z⌕”¦τA“Y∨↷ur1BO#ÿk»⁻υv‽⌊←zω↘^”¦η⁺αβF⁴«⁺⁺⁺α§λιν§μιF⮌…⁰ι«⁺⁺⁺⁺σ§λ⁺κ¹τ§λκν»⁺⁺⁺σ§λ⁰τβ»⁺αη

Try it online!

Link to the verbose version of the code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.