Language Python
(functional, w/o variables or procedures)
Date: | 07/14/05 |
Author: | Ivan Tkatchev |
URL: | n/a |
Comments: | 2 |
Info: | n/a |
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 around,'][abs(cmp(x,0))] })((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', 'Three','Four','Five','Six','Seven','Eight', 'Nine','Ten','Eleven','Twelve','Thirteen','Fourteen', 'Fifteen','Sixteen','Seventeen','Eighteen','Nineteen'][[x,x%10][int(x>=20)]]),'bottle%s of beer' % ['','s'][abs(cmp(x,1))]) for x in xrange(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 | |
using lambda in LISP style | J Adrian Zimmer | 11/14/06 | 2 | |
Exception based | Michael Galpin | 02/08/08 | 0 | |
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
Dr Wood said on 10/18/05 10:58:07
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
How do you figure out how to write this code?