Ninety-Nine Bottles Of Beer
August 5, 2011
Somebody has to be the designated driver, so we’ll give the straight forward solution, but you should feel free to be as creative with your solution as you can. Even though it’s straight forward, the appalling badness of Scheme’s string notation is on glaring display:
(define (beer n)
(define (upword str)
(string-append (string (char-upcase (string-ref str 0)))
(substring str 1 (string-length str))))
(let ((n-words (upword (num->words n)))
(n1-words (upword (num->words (- n 1)))))
(if (< 1 n)
(begin
(display n-words)
(display " bottles of beer on the wall.")
(newline)
(display n-words)
(display " bottles of beer.")
(newline)
(display "Take one down and pass it around.")
(newline)
(display n1-words)
(display " bottles of beer on the wall.")
(newline)
(newline)
(beer (- n 1)))
(begin
(display "Only one bottle of beer on the wall.")
(newline)
(display "Only one bottle of beer.")
(newline)
(display "Take it down and pass it around.")
(newline)
(display "Now there's no more beer on the wall.")
(newline)))))
To run the program, say (beer 99)
.
We wrote the num->words
function in a previous exercise. You can run the program at http://programmingpraxis.codepad.org/TMOKOo5n.
already amazingy done in every possibne way : http://99-bottles-of-beer.net/
UNO is based in one simple principle: #=def, $=print, ?=cond, @=loop is all you need to build any algorithm
Wrote this a while ago for Rosetta Code:
ok, sorry :)
First time poster. But I’ll give it a shot.
using System;
class Program
{
static void Main(string[] args)
{
for (int number = 99; number > 0; number–)
{
int nextNumber = number – 1;
Console.WriteLine("{0} {1} of beer on the wall, {0} {1} of beer…", number, number > 1 ? "bottles":"bottle" );
Console.WriteLine("Take one down, pass it around {0} {1} of beer on the wall.", nextNumber, nextNumber != 1 ? "bottles" : "bottle");
Console.WriteLine();
}//end for
}
}
Try that again.
Implementations in a zillion languages, including Brainf*ck and INTERCAL:
http://99-bottles-of-beer.net/
http://99-bottles-of-beer.net/language-brainfuck-101.html
http://99-bottles-of-beer.net/language-intercal-333.html
My implementation, which uses C++ template metaprogramming with the Boost MPL library, generates the text of the song at compile time.
99bob.cpp
hosted with ❤ by GitHub
It also generates english words instead of digits (i.e. “forty-four” instead of “44”). Not sure if that was part of the brief or not – wasn’t too hard to implement anyway.
Some timing… I recently wrote two versions of the “song”
http://www.emmanueloga.com/2011/07/29/drinking-contest.html
Greetings.
I once SSH’d into my roommate’s mac and ran this little bash script while I was at work and he was home alone.
for ((i=99; i>0 ; i–));
do
let “j = i – 1”;
say $i bottles of beer on the wall;
say $i bottles of beer;
say take one down ;
say pass it around;
say $j bottles of beer on the wall;
done
Then I denied all knowledge of it, and told him the computer must have just been really bored.
def song(num):
if num == 0:
print “Song is over”
elif num == 1:
print str(num) + ” bottle of beer on the wall”
else:
print str(num) + ” bottles of beer on the wall”
song(num-1)
Nothing particularly clever, but FWIW… In PLT Racket.