Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

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

VersionAuthorDateCommentsRate
This example demonstrates the simplicityGerold Penz07/23/0515
Creative versionSchizo11/06/0516
Advanced, extensible beer/wall frameworkJamie Turner05/17/067
minimal versionOliver Xymoron04/20/055
Exception basedMichael Galpin02/08/080
functional, w/o variables or proceduresIvan Tkatchev07/14/052
minimal version with singularEmlyn Jones06/13/053
Fully compliant versionRicardo Garcia Gonzalez01/15/067
Using a iterator classEric Moritz01/20/062
New conditional expressions in 2.5Ezequiel Pochiero12/18/061

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

nallias 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)

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!

Name:

eMail:

URL:

Security Code:
  
Comment: