[Tutor] Beer Bottles

Luke Paireepinart rabidpoobear at gmail.com
Thu Jul 6 11:13:03 CEST 2006


Steve Nelson wrote:
> A bunch of my friends and I have been chatting about "99 bottles of
> beer" - and how to make the shortest code to do it.  I have:
>
> for i in range(100,0,-1):
>    print "%s bottles of beer on the wall, %s bottles of beer\nTake on
> down, pass it around.."%(i,i)
> print "Go to the store, buy some more"
>
> I'm curious to know other ways to handle this - could it be done
> functionally?  How could we obfuscate this (not that we'd want to in
> real life)?  Or make it a (close to) one liner?
>
> Thanks.
>
> S.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   
one way to do it functionally:

def beer(i):a='%s bottles of beer';print "%s on the wall, %s.\nTake one 
down, pass it around.."%(a%i,a%i)
map(beer,range(100,0,-1))
print "Go to the store, buy some more"

definitely check out http://www.99-bottles-of-beer.net

the smallest version there is

By Oliver Xymoron (133 Bytes)

a,t="\n%s bottles of beer on the wall","\nTake one down, pass it around"
for d in range(99,0,-1):print(a%d*2)[:-12]+t+a%(d-1 or'No')


Note both of our examples and Oliver's examples don't handle the 
singular form correctly.
"1 bottles of beer" is not correct. "0 bottles of beer" is pushing it, 
it should be "no bottles of beer."
If you're trying to obfuscate it there are examples of that too,
just check out the python section.
http://www.99-bottles-of-beer.net/language-python-748.html

Enjoy!


More information about the Tutor mailing list