63
votes
\$\begingroup\$

Recreate '99 bottles of beers on the wall'.

The desired output is this:

99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.

98 bottles of beer on the wall, 98 bottles of beer.
Take one down and pass it around, 97 bottles of beer on the wall.

97 bottles of beer on the wall, 97 bottles of beer.
Take one down and pass it around, 96 bottles of beer on the wall.

96 bottles of beer on the wall, 96 bottles of beer.
Take one down and pass it around, 95 bottles of beer on the wall.

95 bottles of beer on the wall, 95 bottles of beer.
Take one down and pass it around, 94 bottles of beer on the wall.

....

3 bottles of beer on the wall, 3 bottles of beer.
Take one down and pass it around, 2 bottles of beer on the wall.

2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.

1 bottle of beer on the wall, 1 bottle of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.

Show me how you would solve this with your favorite programming language. Most creative use of a language wins.

\$\endgroup\$
12
  • 30
    \$\begingroup\$ "Most creative use" is too subjective for an SE site, I think. We should aim to use objective criteria, such as "shortest code". \$\endgroup\$ Jan 27, 2011 at 21:04
  • 5
    \$\begingroup\$ Still a good question though \$\endgroup\$ Jan 27, 2011 at 21:06
  • 3
    \$\begingroup\$ Is it supposed to run infinitely? I assumed so. Also codegolf doesn't own 99 bottles of beer so there is not reason to even discuss it. \$\endgroup\$
    – Mike Bethany
    Jan 28, 2011 at 8:04
  • 1
    \$\begingroup\$ @Chris -- That is going to be the toughest part of this SE site, because the whole thing is based on creativity, because that is half the fun of programming puzzles. The most creative solution isn't always the most efficient or shortest. \$\endgroup\$ Jan 28, 2011 at 14:13
  • 2
    \$\begingroup\$ @Nick: I hear you. I just want to know how one can determine how creative a solution is---based on your own judgement? Based on the voters'? Neither is particularly reliable (no offence). Something like brevity is indisputable---for example, nobody can dispute the "goodness" of the 1-byte stack overflow code golf solution. \$\endgroup\$ Jan 28, 2011 at 14:26

117 Answers 117

2
votes
\$\begingroup\$

D: 310 Characters

import std.stdio, std.string;

void main()
{
    alias writefln p;
    enum b = "%s bottle%s of beer", w = " on the wall";
    auto s = (int r){return format(b, r, r > 1 ? "s" : "");};

    for(int r = 99; r > 0;)
    {
        p("%s%s, %s.", s(r), w, s(r));

        if(--r)
            p("Take one down pass it around, %s%s.\n", s(r), w);
        else
            p("Go to the store and buy some more, %s.", s(99));
    }
}
\$\endgroup\$
2
votes
\$\begingroup\$

PHP (249 chars)

My first golf attempt. It's kinda messy but that was to "fix" my problem with singular/plural in the the third last line of the song.

<?
$l=" on the wall";
$m=" of beer";
$x=" bottles";
for($i=99;$i>=1;$i--){
    $o="$i$x$m$l, $i$x$m.\n";
    $x=($i<3)?" bottle":$x;$o.=($i==1)?"Go to the store and buy some more, 99 
    bottles$m$l.":"Take one down and pass it around, ".($i-1)."$x$m$l.\n\n";
    echo$o;
}

No spaces

<?$l=" on the wall";$m=" of beer";$x=" bottles";for($i=99;$i>=1;$i--){$o="$i$x$m$l, $i$x$m.\n";$x=($i<3)?" bottle":$x;$o.=($i==1)?"Go to the store and buy some more, 99 bottles$m$l.":"Take one down and pass it around, ".($i-1)."$x$m$l.\n\n";echo$o;}

I also want to optimise it more later.

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

PHP (191)

Short tags must be turned on for this to work.

<? eval(gzinflate("]Á\nÂ0De*´ÅkPþ€Ç^»¥Á6+MJñß\rUAzYØÇììŒ4‡†Cȃ;¢)S2&Ê¡'D=™êxÚH{¨k%íq—FQlŸtíi¯NBZidÌ¥)wÑ7Jׄ–£ƒv-îÚ{Ø\0=ñìÚu’6NìEõ\rðûŸ|°fI{f^\"ùÀ-Æf~ÀóHÉQ×ë.ÿ-J¡^o"));?>

For those who are curious, here's what the gzinflate() returns:

$b=' bottles of beer';$w=' on the wall';for($i=99;$i>0;$i--){echo $i>1?"$i$b$w,$b.\nTake one down and pass it around, $i$b$w.\n\n":"1 bottle of beer$w, 1 bottle of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.";}

Broken down (with a few tweaks), that's:

$b=' bottles of beer';
$w=' on the wall';
for($i=99;$i>0;$i--){
   if ($i>1) {
      echo "$i$b$w,$b.\nTake one down and pass it around, $i$b$w.\n\n";
   } else {
      echo "1 bottle of beer$w, 1 bottle of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.";
   }
}

Oddly enough, my original attempt involved using gzinflate() on the inside for the final iteration, but once you encode it again for the eval() it makes it larger than had it not been there.

\$\endgroup\$
2
  • \$\begingroup\$ I think you're supposed to make this a community wiki post, like all the other answers. \$\endgroup\$ Apr 26, 2011 at 14:02
  • \$\begingroup\$ There we go! Fixed it. Thanks for pointing that out. Still new to this stack exchange. \$\endgroup\$
    – Mr. Llama
    Apr 26, 2011 at 20:20
2
votes
\$\begingroup\$

Some pre-processor and C++ template metaprogramming.

#include <stdio.h>
#define x "%d bottle%s of beer on the wall"
#define y ", %d bottle%s of beer."
#define z "\nTake one down and pass it around, "
#define T template
T<int V>struct B{B(){printf(x y z x".\n\n" ,V,"s",V,"s",V-1,V-1==1?"":"s");B<V-1>();}};
T<>struct B<1>{B(){printf(x y "\nGo to the store and buy some more, "x".\n\n",1,"",1,"",99,"s");}};
void main(){B<99>();}
\$\endgroup\$
2
votes
\$\begingroup\$

F# - 214 209 characters

for i=99 downto 1 do stdout.Write("{0}{1}{2}, {0}{1}.\n{3}{1}{2}.\n",i," bottles of beer"," on the wall",if i>1 then"Take one down and pass it around, "+string(i-1)else"Go to the store and buy some more, 99")
\$\endgroup\$
2
votes
\$\begingroup\$

Dart

class Bottle{
  int _amount;
  String _kind,_place;
  get amount() => _amount;
  set amount(int amt){
    if(amt<0){
      _amount = 99;
      throw "Go to store and buy some more, ${toStr(true)}.";
    }
    if(amt>99) throw "I'm drunk.";
    _amount = amt;
  }
  get place() => _place;
  set place(String plc){
    _place = plc;
  }
  get kind() => _kind;
  set kind(String knd){
    _kind = knd;
  }
  Bottle(amt,knd,plc){
    amount = amt;
    kind = knd;
    place = plc;
  }
  String toStr(bool long){
    String plural = amount==1?'':'s';
    String append = long?" on $place":'';
    if(amount>0) return "$amount bottle$plural of $kind$append";
    else return "No bottles of $kind$append";
  }
}
main(){
  Bottle beer = new Bottle(99,'beer','the wall');
  print("${beer.toStr(true)}, ${beer.toStr(false)}.");
  while(beer.amount-->0){
    print("Take one down and pass it around, ${beer.toStr(true)}.");
    print("${beer.toStr(true)}, ${beer.toStr(false)}.");
  }
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ +1 for if(amt>99) throw "I'm drunk.";. \$\endgroup\$
    – awe
    Oct 14, 2011 at 12:17
2
votes
\$\begingroup\$

99

"99" is a language I invented specifically for 99-bottles-of-beer.net

I'm unable to present my program here, since the markdown format doesn't appear to let me show an empty source file. But you probably already have a copy of the program on your system; look for /dev/null or NUL:.

Here's the entry on 99-bottles-of-beer.net, and here's the Github page.

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

Is this cheating? In Python, maybe not beautiful, but short & sweet.

import urllib2
response = urllib2.urlopen('http://pastebin.com/raw.php?i=HCCqYj2z')
html = response.read()
print(html
\$\endgroup\$
2
  • \$\begingroup\$ I think it is :-) \$\endgroup\$ Nov 18, 2013 at 7:07
  • \$\begingroup\$ People have to learn to forbid network access. \$\endgroup\$ Nov 19, 2013 at 8:01
2
votes
\$\begingroup\$

Lilypond - 6435

I can't resist reposting my entry from 99-bottles-of-beer.net. Here is the first part of the output. You can find the rest here.

First eight bars of the song in music notation

Note: The code is obviously not golfed. Moreover, it follows the "official" 99-bottles-of-beer.net lyrics, with the "No more bottles" last verse. Adjusting this if desired is an exercise for the reader.

% Lilypond is a music engraving system with an
% embedded Scheme interpreter.
% http://lilypond.org

\include "english.ly"

% Necessary because we use deep recursion
#(debug-set! stack 0)

\header{
  title =        "Ninety-Nine Bottles of Beer"
  composer =     "Traditional"
  arranger =     \markup{ "Nate Eldredge" }
}

\version "2.8.7"

totalbottles = #99

startmelody = {
  \time 6/8
  \clef treble
  \key c \major
  \autoBeamOff
}

endmelody = {
  \bar "|."
}

% text = \lyricmode {
%   Nine -- ty nine bot -- tles of beer on the wall
%   Nine -- ty nine bot -- tles of beer __
%   Take one down and pass it a -- round
%   Nine -- ty eight bot -- tles of beer on the wall
% }

zerosyl = { }
onesyl = \relative c''{ c4. }
twosyl = \relative c''{ c4 c8 }
threesyl = \relative c''{ c8 c8 c8 }
foursyl = \relative c''{ c16 c16 c8 c8 }
fivesyl = \relative c''{ c16 c16 c8 c16 c16 }

#(define (count-syllables L)
  (if (null? (ly:music-property L 'text))
   (apply + (map count-syllables (ly:music-property L 'elements)))
   1))

% FIXME - check the syllabication
zero = \lyricmode { } % because "seventy" will become { \seventy \zero }
one = \lyricmode { one }
two = \lyricmode { two }
three = \lyricmode { three }
four = \lyricmode { four }
five = \lyricmode { five }
six = \lyricmode { six }
seven = \lyricmode { se -- ven }
eight = \lyricmode { eight }
nine = \lyricmode { nine }
ten = \lyricmode { ten }
eleven = \lyricmode { e -- le -- ven }
twelve = \lyricmode { twelve }
thirteen = \lyricmode { thir -- teen }
fourteen = \lyricmode { four -- teen }
fifteen = \lyricmode { fif -- teen }
sixteen = \lyricmode { six -- teen }
seventeen = \lyricmode { se -- ven -- teen }
eighteen = \lyricmode { eigh -- teen }
nineteen = \lyricmode { nine -- teen }
twenty = \lyricmode { twen -- ty }
thirty = \lyricmode { thir -- ty }
forty = \lyricmode { for -- ty }
fifty = \lyricmode { fif -- ty }
sixty = \lyricmode { six -- ty }
seventy = \lyricmode { se -- ven -- ty }
eighty = \lyricmode { eigh -- ty }
ninety = \lyricmode { nine -- ty }
nomore = \lyricmode { no more }
error = \lyricmode { oh shit }

#(define (speak-small n)
  (cond 
   ((= n 0) zero)
   ((= n 1) one)
   ((= n 2) two)
   ((= n 3) three)
   ((= n 4) four)
   ((= n 5) five)
   ((= n 6) six)
   ((= n 7) seven)
   ((= n 8) eight)
   ((= n 9) nine)
   ((= n 10) ten)
   ((= n 11) eleven)
   ((= n 12) twelve)
   ((= n 13) thirteen)
   ((= n 14) fourteen)
   ((= n 15) fifteen)
   ((= n 16) sixteen)
   ((= n 17) seventeen)
   ((= n 18) eighteen)
   ((= n 19) nineteen)
   (else error)))

#(define (speak-tens n)
  (cond
   ((= n 1) ten)
   ((= n 2) twenty)
   ((= n 3) thirty)
   ((= n 4) forty)
   ((= n 5) fifty)
   ((= n 6) sixty)
   ((= n 7) seventy)
   ((= n 8) eighty)
   ((= n 9) ninety)
   (else error)))

#(define (speak-number n)
  (cond
   ((= n 0) nomore)
   ((< n 20) (speak-small n))
   (else (make-sequential-music 
      (list
       (speak-tens (quotient n 10))
       (speak-small (modulo n 10)))))))

#(define (sing-syllables k)
  (cond
   ((= k 0) zerosyl)
   ((= k 1) onesyl)
   ((= k 2) twosyl)
   ((= k 3) threesyl)
   ((= k 4) foursyl)
   ((= k 5) fivesyl)))

#(define (sing-number n) (sing-syllables (count-syllables (speak-number n))))

SingNumber = #(define-music-function (parser location n)
           (integer?)
           (ly:music-deep-copy (sing-number n)))
SpeakNumber = #(define-music-function (parser location n)
           (integer?)
        (ly:music-deep-copy (speak-number n)) )

#(define (capitalize! s) (string-set! s 0 (char-upcase (string-ref s 0))))
#(define (capitalize s)
  (let ((l (string->list s)))
   (list->string (cons (char-upcase (car l)) (cdr l)))))

% takes a list of music objects
#(define (capitalize-lyric-list L)
  (cond 
   ((null? L))
   ((not (null? (ly:music-property (car L) 'text))) 
    (ly:music-set-property! (car L) 'text 
     (capitalize (ly:music-property (car L) 'text))))
   (else (capitalize-lyric-list
      (append 
       (ly:music-property (car L) 'elements)
       (cdr L))))))

CapitalizeLyric = 
       #(define-music-function (parser location lyr)
     (ly:music?)
     (let ((newlyr (ly:music-deep-copy lyr)))
      (capitalize-lyric-list (list newlyr))
      newlyr))

melodyone =   \relative c''{ g8 g g | c c c c4 r8 | }
melodytwo = \relative c''{ a8 a a | d4.~ d8 r8 r8 | b4 b8 b4 b8 | b8 b b b4 r8 | }
melodytwofinal = \relative c''{ a8 a a | d4.~ d8 r4 | b8 b8 b8 b4 b8 | b4 b8 b4 r8 | }
melodythree = \relative c''{ a8 a b | c c c c4 r8 |}

lyricsone = \lyricmode{ of beer on the wall, }
lyricstwo = \lyricmode{ of beer.  Take one down and pass it a -- round, }
lyricstwofinal = \lyricmode{ of beer.  Go to the store and buy some more, }
lyricsthree = \lyricmode { of beer on the wall. }

bottle = \lyricmode{ bot -- tle }
bottles = \lyricmode{ bot -- tles }

PluralBottle = #(define-music-function (parser location n)
           (integer?)
         (if (= n 1) bottle bottles))


SingVerse = #(define-music-function (parser location n)
          (integer?)
          (let 
           ((nn (if (> n 0) (- n 1) totalbottles))
        (meltwo (if (> n 0) melodytwo melodytwofinal)))
           #{
          { { \SingNumber #$n }}
          \melodyone
           { \transpose c d { \SingNumber #$n }}
          $meltwo
           {  \transpose c' g { \SingNumber #$nn }}
          \melodythree
          #}))

SpeakVerse = #(define-music-function (parser location n)
          (integer?)
          (let 
           ((nn (if (> n 0) (- n 1) totalbottles))
        (lyrtwo (if (> n 0) lyricstwo lyricstwofinal)))
          #{
           \CapitalizeLyric \SpeakNumber #$n
           \PluralBottle #$n
           \lyricsone
           \SpeakNumber #$n
           \PluralBottle #$n
           $lyrtwo
           \SpeakNumber #$nn
           \PluralBottle #$nn
           \lyricsthree
           #}))

SingVerses = #(define-music-function (parser location nstart nend)
          (integer? integer?)
           (if (>= nstart nend)
        (let ((nextverse (- nstart 1)))
         #{ \SingVerse #$nstart \SingVerses #$nextverse #$nend #})
        #{ #} ))

SpeakVerses = #(define-music-function (parser location nstart nend)
          (integer? integer?)
           (if (>= nstart nend)
        (let ((nextverse (- nstart 1)))
         #{ \SpeakVerse #$nstart \SpeakVerses #$nextverse #$nend #})
        #{ #} ))


\score{
  {
    <<
      \new Voice = "one" {
    \transpose c' g  {
      \startmelody
      \SingVerses #totalbottles #0
      \endmelody 
    }
      }
      \new Lyrics \lyricsto "one" { 
    \SpeakVerses #totalbottles #0
      }
    >>
  }
  \layout { }
  \midi { }
}
\$\endgroup\$
2
votes
\$\begingroup\$

PHP 244 226 char

I've been working with PHP for a while, but this is my first crack at code golf. Here's what I came up with

<?php
$b = ' bottles of beer';
$bt = ' bottle of beer';
$w = ' on the wall';
for($i=99;$i>0;$i--)
{
    echo(($i==1)?"$i$bt$w, $i$bt\nGo to the store and buy some more, 99$b$w.":"$i$b$w, $i$b\nTake one down and pass it around, ($i-1)$b$w.\n\n");
}

Golfed it some more!

<?php
$w=' on the wall';
for($i=99;$i>0;$i--)
{
    $b =' bottle'.(($i==1)?'':'s').' of beer';
    echo"$i$b$w, $i$b\n".(($i==1)?"Go to the store and buy some more, 99$b$w.":"Take one down and pass it around, ($i-1)$b$w.\n\n");
}
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to Programming Puzzles and Code Golf! \$\endgroup\$ Aug 16, 2014 at 7:08
  • \$\begingroup\$ I don\t know PHP too well, but would for($i=99;$i-->0;) work? Also, remove the unnecessary whitespace in assignments, $b=' bottles of beer'; \$\endgroup\$
    – seequ
    Aug 16, 2014 at 7:58
2
votes
\$\begingroup\$

oOo CODE: 6436 Characters

Self documenting code on its finest. It's based on the brainfuck answer. Din't have time for fancy ascii art so here it is:

99 botTlES oF beER oN thE waLL, 99 bottleS of beEr, takE onE doWN aND pAss iT aROuND, 98 bottlEs oF bEer on tHE walL.
98 bottleS of bEeR on tHe wAlL, 98 bottleS oF beER, taKe oNe dOwN anD paSS iT arOund, 97 bottlES oF beER oN thE waLL.
97 bottles oF bEEr oN tHE wALl, 97 bottleS of bEeR, taKE oNe down aNd pAsS it arouNd, 96 bottleS of beer on tHe wAlL.
96 bottlEs oF beER on tHE walL, 96 bottles oF bEEr, take onE doWN aND pASs iT aROuND, 95 bottles oF bEEr oN tHE wALl.
95 bottleS of bEeR on tHe wAlL, 95 bottlES oF beER, taKe oNe dOwN anD paSS iT arOUnD, 94 bottlES oF beER oN thE waLL.
94 bottlEs oF bEEr oN tHe waLl, 94 bottleS of bEeR, taKE oNE dOWn aNd pAsS it arouNd, 93 bottleS of bEeR on tHe wAlL.
93 bottlES oF beEr on thE waLL, 93 bottlEs oF bEEr, takE onE doWN aND pASs it arOuND, 92 bottlEs oF bEEr oN tHE wALl.
92 bottleS of bEeR on tHe wAlL, 92 bottlES oF beER, taKe oNe dOwN anD paSS iT aroUnD, 91 bottlES oF beER oN thE waLL.
91 bottlEs oF bEEr on thE wALl, 91 bottleS of bEeR, taKE oNE dOWn aNd pAsS it arouNd, 90 bottleS of bEeR on tHe wAlL.
89 bottlES oF beER oN the waLL, 89 bottlEs oF bEEr, takE onE doWN aND pASs iT aROuNd, 88 bottlEs oF bEEr oN tHE wALl.
88 bottleS of bEeR on tHe wAlL, 88 bottlES oF beER, taKe oNe dOwN anD paSS iT arOUnD, 87 bottlEs of beER oN thE waLL.
87 bottlEs oF bEEr oN tHE wALl, 87 bottleS of bEeR, taKE oNE dOWn aNd pAsS it aRoUNd, 86 bottleS of bEeR on tHe wAlL.
86 bottlES oF beER oN thE waLL, 86 bottlEs oF bEEr, takE onE doWn aNd pAss it aRouNd, 85 bottles of bEer on tHe wAll.
85 bottles oF beer on the wAlL, 85 bottleS oF beER, taKe one dOwN anD paSS iT arOund, 84 bottlES oF beER oN thE wall.
84 bottles oF bEEr oN tHe waLl, 84 bottleS of beer, taKE oNE dOWn aNd pAsS it aRoUNd, 83 bottleS of bEeR on the wall.
83 bottlEs of beER oN thE walL, 83 bottlEs oF bEer, takE onE doWN aND pAss iT aROuND, 82 bottlEs oF bEEr on tHe wAll.
82 bottleS of beeR on the walL, 82 bottlEs oF beEr, take one dowN anD paSs iT arOunD, 81 bottles of beEr oN thE wAll.
81 bottles of beer on thE wall, 81 bottles of beer, take one down and pass it around, 80 bottlEs of beer on the wALl.
79 bottlEs oF beEr oN thE waLL, 79 bottles of bEer, takE oNe down and pass it around, 78 bottles of beER on tHe wAll.
78 bottleS of beeR on the walL, 78 bottlEs oF beEr, take one dOWn and pass it around, 77 bottles of beer on the wall.
77 bottlES of bEer on tHe wAll, 77 bottlEs of beeR, taKe oNe dOwn and pasS iT around, 76 bottles of bEEr on the wALl.
76 bottles of beer on the wall, 76 bottles oF beer, takE onE doWn aNd pAss it aRouNd, 75 bottles of bEer on tHe wALL.
75 bottles of beer on the wall, 75 bottles of beer, take one dOWn anD pAss iT arOunD, 74 bottlEs oF beEr oN thE waLl.
74 bottles oF beer on the wall, 74 bottles of beer, take one down and pass it around, 73 bottlEs of beeR on the walL.
73 bottlEs oF beER on thE waLL, 73 bottles of bEer, takE onE doWN and pass it around, 72 bottles of beer on the wall.
72 bottles of beer on the wall, 72 bottles oF beEr, take one dowN anD paSs iT arOunD, 71 bottlEs oF beEr oN tHe wall.
71 bottles of beer on the waLL, 71 bottleS of beeR, taKe oNe dOwn and pasS it aroUND, 70 bottlEs of beeR on the walL.
69 bottleS of beer on the walL, 69 bottles of beer, take oNe down and pass it around, 68 bottles oF beer on the wall.
68 bottlEs of beeR on the walL, 68 bottles oF beEr, take one dOWn and pass it around, 67 bottles of beer on tHe waLl.
67 bottles of bEer on tHe wAll, 67 bottleS of beeR, taKe oNe dOwn aND pass it around, 66 bottles of beer on the wall.
66 bottles of bEer oN thE waLl, 66 bottles oF beer, takE onE doWn aNd pAss it aROUnd, 65 bottles of beer oN the wall.
65 bottlEs of beer on the wall, 65 bottles of beeR, take one dowN anD paSs iT arOunD, 64 bottlEs oF beEr oN thE waLL.
64 bottlES of beer on the wall, 64 bottles of beer, take one down and pass it around, 63 bottleS of beeR on the walL.
63 bottlEs oF beEr oN thE waLl, 63 bottles of beer, take one down anD pass it aRouNd, 62 bottles of bEer on tHe wAll.
62 bottlEs of beer on the wall, 62 bottles of beer, take one down and pAss iT aRounD, 61 bottlEs oF beEr oN tHe waLl.
61 bottles of bEer on tHE wall, 61 bottles of beer, takE one down and pass it around, 60 bottlEs of beer on the wall.
59 bottles oF beEr oN thE waLl, 59 bottlES of bEer, takE onE doWn aNd pAss it aRouNd, 58 bottles oF beer on the wall.
58 bottles of beer on the wall, 58 bottles of bEer, take one dowN anD paSs iT aRounD, 57 bottlEs oF beEr oN tHe wall.
57 bottles of beer on the wall, 57 bottles of beer, take onE down and pasS it aroUnd, 56 bottleS of beeR on tHE wall.
56 bottles of beer on the wall, 56 bottles of beer, taKe oNe doWn aNd pAss it aRouNd, 55 bottles of bEer on tHe wAll.
55 bottleS of bEEr on the wall, 55 bottles of beer, take one dOWn and pass it around, 54 bottles of bEer oN thE waLl.
54 bottles of bEer on tHe wAll, 54 bottleS of beeR, taKe oNe dOWN and pass it arouND, 53 bottles of beer on the wall.
53 bottles of beer on tHe waLl, 53 bottles of beer, take one dOwn aNd pAss it aRouNd, 52 bottles of bEer on tHe wAll.
52 bottlEs of beeR on the walL, 52 bottlES of beEr, take one dowN aNd pasS it aRound, 51 bottles of bEer on the wall.
51 bottlES of beer on the wall, 51 bottles of beer, take one down and pASs iT aroUnd, 50 bottleS of beeR oN the walL.
49 bottlEs oF bEer on the wall, 49 bottles of beer, take one dOwn aNd pAss it aRouNd, 48 bottles of bEer on tHe wAll.
48 bottleS of beeR oN the wall, 48 bottles of beer, take one down and pass it aroUNd, 47 bottlEs oF beEr oN thE wAll.
47 bottles of bEer on tHe wAll, 47 bottles of beer, take onE down and pASs iT around, 46 bottles of beer on the wall.
46 bottlEs oF beEr oN thE waLl, 46 bottles of bEer, takE onE doWn aNd pASS it around, 45 bottles of beer on the wall.
45 bottles of beer oN the wALl, 45 bottlEs oF beEr, take one dowN anD paSs iT arOunD, 44 bottles of beer on the wall.
44 bottles of beer on the wall, 44 bottles of beer, take oNe dOwn and pasS it aroUnd, 43 bottlEs of beeR oN the walL.
43 bottlEs oF beEr oN tHe wall, 43 bottles of beer, take one down and pass it around, 42 bottles of beer on thE wall.
42 bottleS of beeR on the walL, 42 bottlEs oF beEr, take one dowN aNd pass it around, 41 bottles of beeR on tHe waLl.
41 bottles of bEer on tHe wAll, 41 bottlEs of beeR, take oNe dOWn anD pAsS it aRoUNd, 40 bottleS of bEeR on the walL.
39 bottlES oF beER oN thE waLL, 39 bottles oF beer, take one dOW
\$\endgroup\$
2
votes
\$\begingroup\$

CJam, 184 characters

100,{[:A" bottles of beer":B" on the wall":C", "AB".
Take one down and pass it around, "A(BC".

"]+}%W%-2<)-3" bottle of beer":Dt1DC", "1D".
Go to the store and buy some more, "99BC"."

Try it online here

\$\endgroup\$
2
  • \$\begingroup\$ Not sure if CJam is allowed here. The question was asked on 2011-01-27, while CJam's initial release was published on SourceForge on 2014-04-05. (I upvote it anyway for founding a language not used in the previous 113 answers.) \$\endgroup\$
    – manatwork
    Dec 15, 2014 at 13:03
  • 1
    \$\begingroup\$ Not contending for an accepted answer :) and yes, the answer is invalid. \$\endgroup\$
    – Optimizer
    Dec 15, 2014 at 13:04
2
votes
\$\begingroup\$

ArnoldC

IT'S SHOWTIME
HEY CHRISTMAS TREE b
YOU SET US UP 99
HEY CHRISTMAS TREE a
YOU SET US UP 1
STICK AROUND a
TALK TO THE HAND b
TALK TO THE HAND "bottles of beer on the wall,"
TALK TO THE HAND b
TALK TO THE HAND "bottles of beer. Take one down, pass it around,"
GET TO THE CHOPPER b
HERE IS MY INVITATION b
GET DOWN 1
ENOUGH TALK
TALK TO THE HAND b
TALK TO THE HAND "bottles of beer on the wall."
GET TO THE CHOPPER a
HERE IS MY INVITATION b
LET OFF SOME STEAM BENNET 2
ENOUGH TALK
CHILL
TALK TO THE HAND "1 bottle of beer on the wall, 1 bottle of beer. Take one down and pass it around, no more bottles of beer on the wall."
TALK TO THE HAND "No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall."
YOU HAVE BEEN TERMINATED

"Milk is for babies. When you grow up you have to drink beer." -Arnold Schwarzenegger

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

JavaScript (291 characters):

function w(b){document.writeln(b+"<br/>")}function s(b){return b+" bottle"+(b==1?"":"s")}for(var c=99,o=" of beer",a="around",l=" on the wall",p=".";c>0;){w(s(c)+o+l+", "+s(c)+o+".");--c>0?w("Take one down, pass it "+a+", "+s(c)+o+l+p):w("Go to the store, and buy some more, "+s(99)+o+l+p)};
\$\endgroup\$
1
vote
\$\begingroup\$

C

#include <stdio.h>
#include <stdlib.h>

void printBottles(int i, char * pP)
{
    printf("%d bottl%s of beer on the wall, %d bottl%s of beer.\n",i,pP,i,pP);
}
void printTakeBottleDown(int i)
{
    printf("Take one down and pass it around, %d bottles of beer on the wall.\n\n",i);
}

int main()
{
    int i = 99;

    while(i>1)
    {
        printBottles(i--,"es");
        printTakeBottleDown(i);
    }

    printBottles(1,"e");
    printf("Go to the store and by some more, 99 bottles of beer on the wall.");

    return 0;
}
\$\endgroup\$
1
vote
\$\begingroup\$

PHP, 209 characters

<? $a=" bottle";$b=" of beer";$c=" on the wall";for($i=100;$i>1;)echo--$i>1?$i.$a."s$b$c, $i$a"."s$b.\nTake one down and pass it around, ":"$i$a$b$c, $i$a$b.\nGo to the store and buy some more, 99$a"."s$b$c.";

Output is shown here: http://codepad.viper-7.com/wmknQl.php53_t

Instead of using the third section in a for loop, it decreases the number on the fly when it is determining what string it needs to print out.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ That's... not the output expected (from the original post) although maybe an alternate variation of "99 Bottles". \$\endgroup\$
    – Mr. Llama
    Apr 25, 2011 at 22:27
1
vote
\$\begingroup\$

Python 3.1(268):

b=99
a=" bottles of beer"
w=" on the wall"

while 1:
 for i in range(0,99):
  print(`b`+a+w+", "+`b`+a+".")
  if b>0:
   print(" Take one down and pass it around, "+`(b-1)`+a+w+".")
   b-=1
  else:
   b=99
   print(" go to the store and buy some more, "+`(b)`+a+w+".")
\$\endgroup\$
5
  • \$\begingroup\$ What's the problem? \$\endgroup\$
    – John
    Jan 30, 2011 at 3:08
  • \$\begingroup\$ I wasn't the downvoter, however, your program's output does not conform to the spec. Read what happens after 1---it loops back to 99, but with different text. \$\endgroup\$ Jan 30, 2011 at 3:26
  • \$\begingroup\$ @Chris: Could you give me your output please? On my machine, it stops at 0 bottles of beer on the wall like it's supposed to. \$\endgroup\$
    – John
    Jan 30, 2011 at 3:41
  • \$\begingroup\$ It's not supposed to stop at 0. It's supposed to loop back to 99 with text "Go to the store and buy some more", and run forever. Read the question again. \$\endgroup\$ Jan 30, 2011 at 3:42
  • \$\begingroup\$ @Chris: IMHO, it was not very clear that it was supposed to loop forever(he never says that), but I fixed it now. \$\endgroup\$
    – John
    Jan 30, 2011 at 18:00
1
vote
\$\begingroup\$

Python (284 chars)

o=1;b,w,f,s,l='bottle%s of beer|on the wall|%d %s %s, %d %s.\n|Take one down and pass it around, %d %s %s.\n\n|Go to the store and buy some more, 99 %s %s.\n'.split('|');c='s';d='';j=99;k='';i=j
while i>o:
 k+=f%(i,b%c,w,i,b%c)+s%(i-o,b%c,w)
 i-=o
k+=f%(o,b%d,w,o,b%d)+l%(b%c,w);print k,
\$\endgroup\$
1
vote
\$\begingroup\$

Python 3.2 (256)

import sys
a,b,c,d=" bottles of beer"," on the wall","1 bottle of beer",".\n"
[print(str(i)+a+b+', '+str(i)+a+d+'Take one down and pass it around, '+str(i-1)+a+b+d) for i in range(99,1,-1)]
print(c+b+', '+c+d+'Go to the store and buy some more, 99'+a+b+d)

Readability sucks though. :|

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

F#, 280 bytes

let p,w=printfn," on the wall"
let a n=sprintf"%d bottle%s of beer"n (if n=1 then""else"s")
let rec f=function 1->p"Go to the store and buy some more, %s%s."(a 99)w|n->p"Take one down and pass it around, %s%s.\n"(a(n-1))w;g(n-1)
and g n=let b=a n in p"%s%s, %s."b w b;f n
g 99

Somewhat decompressed version:

let printfunc = printfn

let wallstring = " on the wall"

let inventory n = sprintf "%d bottle%s of beer" n (if n=1 then "" else "s")

// let rec..and signifies mutual recursion. Without the 'and' keyword
// nextaction won't know about displaybeers which is defined later.
let rec nextaction = function
    | 1 -> // oh noes! last bottle!
        printfunc "Go to the store and buy some more, %s%s." (inventory 99) wallstring
    | n -> // keep guzzling!
        printfunc "Take one down and pass it around, %s%s.\n" (inventory (n-1)) wallstring
        displaybeers (n-1)

and displaybeers n = 
    let beerstring = inventory n
    printfunc "%s%s, %s." beerstring wallstring beerstring
    nextaction n

// let's have a party
displaybeers 99 
\$\endgroup\$
1
vote
\$\begingroup\$

Python with recursivity...

q=lambda e:' bottle%s of beer '%e
w='on the wall'
r='s'
def b(x):
    if x==1:
        print'1%s%s, 1%s\nGo to the store and buy some more, 99%s%s'%(q(''),w,q(''),q(r),w)
    else:
        print'%s%s%s, %s%s\nTake one down and pass it around, %s%s%s'%(x,q(r),w,x,q(r),x-1,q(r),w)
        b(x-1)
b(99)
\$\endgroup\$
1
vote
\$\begingroup\$

Mathematica 7, 281

b[i_]:=If[i==1,ToString@i<>" bottle",ToString@i<>" bottles"]
w=" of beer on the wall";
s[i_]:=If[i==0,".
   Go to the store and buy some more, "<>b@99<>w<>".",
  ".
   Take one down and pass it around, "<>b@i<>w<>"."]
j:=100-i
Do[Print[b@j<>w<>", "<>b@j<>StringDrop[w,-12]<> 
   s@(j-1)],{i,99}]
\$\endgroup\$
1
vote
\$\begingroup\$

PHP, 229 223 chars

<?$a=" bottle";$b=" of beer";$c=" on the wall";for($i=99;$i>1;)echo"$i{$a}s$b$c, $i{$a}s$b.\nTake one down and pass it around, ".--$i.$a."s$b$c.\n\n";echo"$i$a$b$c, $i$a$b.\nGo to the store and buy some more, 99{$a}s$b$c.";

Not very glamorous though.

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

Python 2 (1129 1120 chars)

import zlib,base64
print zlib.decompress(base64.b64decode(b"eF6dmk1u2mAABfec4jtAhJr/cIJeoBcA4apVCa6AKOrtiyJ1ldeZJ2/t8ZvdCOxvsxm7+XI5TOcxfx+7aTqN+TguP6bxvj0cbsbm0/316tv213SlprGf349je9yP39vzefy8jO1pfjvur0+90Op6tWIgDVTaZ9MikAYq7ZNpEUgDlfbRtAikgUr7YFoE0kClvTctAmmg0t6ZFoE0UGlvTYtAGqi0X0yLQBpotC8YoauWgTRQaa1SDKSBSmuVYiANVFqrFANpoNJapRhIA5XWKsVAGqi0VikG0kCltUoxkAYqrVWKgTRQaa1SDKSBRvtslWIgDVRaqxQDaaDSWqUYSAOV1irFQBqotFYpBtJApbVKMZAGKq1VioE0UGmtUgykgUprlWIgDVRaqxQDaaDRPlmlGEgDldYqxUAaqLRWKQbSQKW1SjGQBiqtVYqBNFBprVIMpIFKa5ViIA1UWqsUA2mg0lqlGEgDldYqxUAaaLSPVikG0kCltUoxkAYqrVWKgTRQaa1SDKSBSmuVYiANVFqrFANpoNJapRhIA5XWKsVAGqi0VikG0kCltUoxkAYa7YNVioE0UGmtUgykgUprlWIgDVRaqxQDaaDSWqUYSAOV1irFQBqotFYpBtJApbVKMZAGKq1VioE0UGmtUgykgUZ7b5ViIA1UWqsUA2mg0lqlGEgDldYqxUAaqLRWKQbSQKW1SjGQBiqtVYqBNFBprVIMpIFKa5ViIA1UWqsUA2mg0d5ZpRhIA5XWKsVAGqi0VikG0kCltUoxkAYqrVWKgTRQaa1SDKSBSmuVYiANVFqrFANpoNJapRhIA5XWKsVAGmi0t1YpBtJApbVKMZAGKq1VioE0UGmtUgykgUprlWIgDVRaqxQDaaDSWqUYSAOV1irFQBqotFYpBtJApbVKMZAGGq1Fys50LnFaoeyo1BKn5clOICxxWpvsw94Sp4XJ3pcvcVqV7DXUEqclyf7dLXFaj+xH0xLnv5b8Lwpw+/PD69XXeVzmD+J8mU/Th3L39mec59dpvF6v3Azr0F83Y1CM")).decode()
\$\endgroup\$
1
vote
\$\begingroup\$

PYTHON 221 CHARS

a="%d bottles of beer on the wall"
b="Take one down and pass it around, "
c="Go to the store and buy some more, %s."%a
d=', '+ a[0:18]+'.\n'
for i in range(99,1,-1):print(a+d+b+a)%(i,i,i-1)+'.\n'
print(a+d+c%99)%(i-1,i-1)
\$\endgroup\$
1
vote
\$\begingroup\$

C++, 266 chars

int b=99;string m=" bottles of beer on the wall, ";string n=" bottles of beer, take one down and pass it around, ";string r=" bottles of beer.\n";void p(int b){cout<<b<<m<<b<<n<<--b<<r;}while(b!=0){p(b);b--;if(b<1){cout<<"\ngo to the store and buy some more.";b=99;}  

This assumes that <iostream> is #included

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

Scala, 290

def b(i:Int,j:Int)=(i+" bottle"+(if(i==1)""else"s"))+(if(j==1)" of beer on the wall"else"");(1 to 99).reverse.map{i => b(i,1)+", "+b(i,0)+" of beer.\n"+(if(i == 1) "Go to the store and buy some more, "+b(99,1)+"."else"Take one down and pass it around, "+b(i-1,1)+".\n")}.foreach{println(_)}
\$\endgroup\$
1
vote
\$\begingroup\$

C

#define $ " bottles of beer"
#define _ $" on the wall"
main(t){for(t=99;t;printf("%d"_", %d"$".\n",t,t),printf(t?"Take one down and pass it around, %d"_"\n":"Go to the store and buy some more, 99"_"\n",t---1));}

(NOTE: should compile fine "as is" in gcc, however due to the t-- undefined behavior, on vs one must replace "t?" with "t>1?", 2 bytes longer =;)

\$\endgroup\$
1
  • \$\begingroup\$ If it is indeed undefined (and not implementation-defined) behavior then one should treat the whole program as just accidentally doing what you intended. \$\endgroup\$
    – Joey
    May 30, 2011 at 14:24
1
vote
\$\begingroup\$

C - 297 characters

My first golfing attempt! The only "bug" I can think of is that it prints a double \n for the final sentence too. A few characters could be saved if the C preprocessor could have substituted macros inside printf strings, but apparently it can't.

#include <stdio.h>
#define y a,i-1?"s":""
main(){int i;char*a=" bottle",*b=" of beer",*c=" on the wall";for(i=99;i!=0;i--)printf("%d%s%s%s%s, %d%s%s%s.\n%s, %d%s%s%s%s.\n\n",i,y,b,c,i,y,b,i-1?"Take one down and pass it around":"Go to the store and buy some more",i-1?i-1:99,a,i?"s":"",b,c);}
\$\endgroup\$
1
vote
\$\begingroup\$

C#, 289 286 275 274 characters

Didn't do anything creative, just tried to make it small.

Update: I've taken a different approach and shaved off another 11 12 characters. Still thinking about it... haven't given up and googled it yet.

class 
P{static 
void Main(){
for(int i=99;i>0;
)System.Console.Write
(i+"{1}{2}{3}, {0}{1}{2} of beer.\n"+(i>1?
"Take one down and pass it around, {4}{1}{5}{3}.\n\n":
"Go to the store and buy some more, 99{1}s{3}."
),i--," bottle",i>0?"s":"",
" of beer on the wall"
,i,i>1?"s":""
);}}

Earlier version:

class P{
static void 
Main(){for(int 
i=99;i>0;){string a
=" bottle",b=" of beer",c=
" on the wall",e=i+a+(i>1?"s":"")+
b;System.Console.Write(e+c+", "+e+".\n"+(i-->
1?"Take one down and pass it around, "+i+a+(i>1?"s":"")+b
+c+".\n\n":"Go to the store and buy some more, 99"+a+"s"+b+c+"."));}}}
\$\endgroup\$
6
  • \$\begingroup\$ Good job, and keep going. This can be done in 232 chars in C#. \$\endgroup\$
    – nharren
    Jun 1, 2011 at 0:33
  • \$\begingroup\$ This might help: link \$\endgroup\$
    – nharren
    Jun 1, 2011 at 0:40
  • \$\begingroup\$ Oh man, 232? Now I'm gonna have to keep working on it. Will that link give me the answer? I want to try to figure it out myself first. \$\endgroup\$ Jun 1, 2011 at 1:25
  • \$\begingroup\$ No it is just a hint. \$\endgroup\$
    – nharren
    Jun 1, 2011 at 1:35
  • \$\begingroup\$ You can take the braces off the for loop. \$\endgroup\$
    – nharren
    Jun 2, 2011 at 5:45

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