real language

Bookmarking Digg Diigo DZone Earthlink Google
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language Python

(functional, w/o variables or procedures)

Author:Ivan Tkatchev
Score: (2.79 in 215 votes)
## Functional Python:
## Fully-features '99 bottles' with proper English grammar and English numerals.
## Written without procedures or side-effects; no assignment was used except
## that which is inherent in generator expressions and lambda calls.
## Nicely fits on one (long) line. :)

print ''.join('%(pre)s%(num)s %(bot)s on the wall, %(nul)s %(bot)s,\n%(tak)s\n' % (lambda c,b:
{'pre':['','%s %s on the wall.\n\n' % (c,b)][abs(cmp(c,'Ninety-nine'))],
 'num':c, 'nul':c.lower(), 'bot':b,
 'tak':['Go to the store and buy some more... Ninety-nine %s.' % b,'Take one down, pass it
})((lambda x,o: [(['Twenty','Thirty','Forty','Fifty',
'Sixty','Seventy','Eighty','Ninety'][x/10-2]+'-'+o.lower()).replace('-no more',''),
o][int(x<20)])(x, ['No more','One','Two',
'Fifteen','Sixteen','Seventeen','Eighteen','Nineteen'][[x,x%10][int(x>=20)]]),'bottle%s of beer' %
for x in xrange(99,-1,-1))

Download Source | Write Comment

Alternative Versions

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
using lambda in LISP styleJ Adrian Zimmer11/14/062
Fully compliant versionRicardo Garcia Gonzalez01/15/067
minimal version with singularEmlyn Jones06/13/053
Exception basedMichael Galpin02/08/080
Using a iterator classEric Moritz01/20/062
New conditional expressions in 2.5Ezequiel Pochiero12/18/061


>>  Dr Wood said on 10/18/05 10:58:07

Dr Wood In order to get this to work on Python 2.3.5 I had to put a '[' after the first '(' and a ']' before the last ')'. It works either way with Python 2.4.1.

>>  Steven Bluen said on 03/27/10 08:30:53

Steven Bluen How do you figure out how to write this code?

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!




Security Code: