some code-golfing …

Filed under amusing, artistic, coding, perl

Yesterday I discovered a funny challenge on a board I visit regularly. The challenge was to create the shortest possible program to get a given output. In my case, it was the “99 bottles of beer” song. Here is my solution:

$b=" on the wall.";for(reverse 1..99)
{$i=$_>1?" bottles of beer":" bottle of beer";
print"$_$i$b $_$i.\n";print"Take one down and pass it around,
",$_-1,"$i$b\n\n"if$_>1;}
print"Go to the store and buy some more, 99 bottles of beer$b\n";

236 characters (you can delete the newlines. I just put them in to make it more readable). So far I couldn’t get it any shorter. Maybe someone else could give it a try. ;)

Ok, just did a short update. Now it has 235 characters:

$b=" on the wall.";for(reverse 1..99){print$_,$i=$_>1?
" bottles of beer":" bottle of beer",
"$b $_$i.\n";print"Take one down and pass it around, ",
$_-1,"$i$b\n\n"if$_>1;}
print"Go to the store and buy some more, 99 bottles of beer$b\n";

FINAL UPDATE:
#####################

I just discovered a small singular/plural problem with the code above, so I had to redesign it a little … This is now the final (and working) code:

$i=" bottles of beer";$b=" on the wall";for(reverse 1..99)
{$j=$i if$_>1;print"$_$j$b, $_$j.\n";$j=~s/es/e/ if$_==2;
print"Take one down and pass it around, ",$_-1,"$j$b.\n
\n"if$_>1;}
print"Go to the store and buy some more, 99$i$b.\n";

234 characters …

4 Comments

  1. chaos says:

    lol, silly,but great =]

  2. chaos says:

    i did it with 71 characters ;)

    here’s my python script:

    import urllib
    print urllib.urlopen(”http://tinyurl.com/5qlwux”).read()

    lol =] but it works : )

  3. ap0calypse says:

    I know, some people did that, but it’s not allowed in the rules.
    You have to write everything yourself. Give it a try :) !

  4. Alex says:

    yep, including anything is not allowed.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*