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

(Fully compliant version)

Date:01/15/06
Author:Ricardo Garcia Gonzalez
URL:n/a
Comments:5
Info:http://www.python.org/
Score: (3.18 in 33 votes)
#!/usr/bin/env python
# 	99 bottles of beer in Python by Ricardo Garcia. Public Domain code.
# 	Fully compliant version, pretty indentation, fits in 80x24.

plural = lambda n: n != 1 and "s" or ""
number = lambda n: n == 0 and "No" or str(n)
next = lambda n: (n - 1) % 100

pu_line = lambda n: n == 0 and	"Go to the store and buy some more" or	\
				"Take one down, pass it around"

verses = lambda n:	"%s bottle%s of beer on the wall!\n"		\
			"%s bottle%s of beer!\n"			\
			"%s\n"						\
			"%s bottle%s of beer on the wall!\n" %		\
			(	number(n), plural(n),
			 	number(n), plural(n),
			 	pu_line(n),
			 	number(next(n)), plural(next(n))	)

print "\n".join([verses(x) for x in range(99, -1, -1)])

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
This example demonstrates the simplicityGerold Penz07/23/058
Creative versionSchizo11/06/0513
minimal versionOliver Xymoron04/20/053
Advanced, extensible beer/wall frameworkJamie Turner05/17/064
Exception basedMichael Galpin02/08/080
Using a iterator classEric Moritz01/20/062
New conditional expressions in 2.5Ezequiel Pochiero12/18/060
using lambda in LISP styleJ Adrian Zimmer11/14/060
functional, w/o variables or proceduresIvan Tkatchev07/14/051
minimal version with singularEmlyn Jones06/13/052

Comments

>>  gp said on 12/08/06 23:17:13

gp very pythonic except "and/or" paradigm. maybe it's time to rewrite it for 2.5 using "a if cond else b"?

>>  Ricardo Garcia Gonzalez said on 12/31/06 15:17:13

Ricardo Garcia Gonzalez Indeed. I should have probably omitted the brackets in the last line, and used xrange instead of range. :)

>>  LR said on 03/19/07 15:51:45

LR Calling the code above "pythonic" is not really helpful. Python is an very readable language, but this example is anything but.

>>  Ricardo Garcia Gonzalez said on 03/29/07 19:15:13

Ricardo Garcia Gonzalez I have updated the script for Python 2.5. I'm not sure I should post it as a new version, but in the mean time, I'll use the comments section.

<pre>
#!/usr/bin/env python
# 99 bottles of beer in Python by Ricardo Garcia. Public Domain code.
# Fully compliant version, pretty indentation, fits in 80x24.
# This version requires Python 2.5.

def plural(n): return ('s' if n != 1 else '')
def numtxt(n): return (str(n) if n != 0 else 'No')
def next(n): return ((n - 1) % 100)

def line_1_4(n): return ('%s bottle%s of beer on the wall!'
% (numtxt(n), plural(n)))

def line_2(n): return ('%s bottle%s of beer!'
% (numtxt(n), plural(n)))

def line_3(n): return ('Take one down, pass it around' if n != 0
else 'Go to the store and buy some more')

def verses(n): return ('%s\n%s\n%s\n%s\n'
% (line_1_4(n), line_2(n),
line_3(n), line_1_4(next(n))))

print '\n'.join(verses(x) for x in xrange(99, -1, -1))
</pre>

>>  Ricardo Garcia Gonzalez said on 03/29/07 19:16:22

Ricardo Garcia Gonzalez Aw, fuck. I suspected it was going to screw it up... and it did. Sorry. :(

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: