Language Python
(using lambda in LISP style)
Date: | 11/14/06 |
Author: | J Adrian Zimmer |
URL: | http://jazimmer.net |
Comments: | 2 |
Info: | http://python.org |
Score: | (2.86 in 36 votes) |
# Readable Python Version of "99 Bottles of Beer" Program # Well, its readable if you know Python reasonably well. # Public Domain by J Adrian Zimmer [[ jazimmer.net ]] verse1 = lambda x: \ """%s of beer on the wall, %s of beer. Take one down, pass it around, %s of beer on the wall. """ % (bottle(x),bottle(x),bottle(x-1)) verse2 = \ """No more bottles of beer on the wall, no more bottles of beer. Go to the store, buy some more, 99 bottles of beer on the wall. """ def verse(x): if x==0: return verse2 else: return verse1(x) def bottle(x): if x==0: return "no more bottles" elif x==1: return str(x) + " bottle" else: return str(x) + " bottles" print "\n".join( [ verse(x) for x in range(99,-1,-1) ] )
Download Source | Write Comment
Alternative Versions
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
This example demonstrates the simplicity | Gerold Penz | 07/23/05 | 15 | |
Creative version | Schizo | 11/06/05 | 16 | |
Advanced, extensible beer/wall framework | Jamie Turner | 05/17/06 | 7 | |
minimal version | Oliver Xymoron | 04/20/05 | 5 | |
Exception based | Michael Galpin | 02/08/08 | 0 | |
functional, w/o variables or procedures | Ivan Tkatchev | 07/14/05 | 2 | |
minimal version with singular | Emlyn Jones | 06/13/05 | 3 | |
Fully compliant version | Ricardo Garcia Gonzalez | 01/15/06 | 7 | |
Using a iterator class | Eric Moritz | 01/20/06 | 2 | |
New conditional expressions in 2.5 | Ezequiel Pochiero | 12/18/06 | 1 |
Download Source | Write Comment
Add Comment
Please provide a value for the fields Name,
Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.
Please don't post large portions of code here! Use the form to submit new examples or updates instead!
Comments
said on 07/15/09 22:36:11
It could be made more functional:
def verse(x):
return verse2 if x == 0 else verse1 (x)
def bottle(x):
return "no more bottles" if x == 0 else "%d bottle%s" % (x, "" if x == 1 else "s"
nallias said on 12/02/09 09:20:16
Another version of a Lisp-like code:
i=0
wall=(i==1) and (lambda i: str(i)+" bottle of beer on the wall, "+str(i)+" bottle of beer!\nTake one down, pass it around,\n"+str(i-1)+" bottles of beer on the wall!\n" or (lambda i: str(i)+" bottle of beer on the wall, "+str(i)+" bottle of beer!\nTake one down, pass it around,\nNo bottles of beer on the wall!\n"
for i in range(99,0,-1):
print wall(i)