Programs like this were posted on this thread: > def fib(): > generation, parent_rabbits, baby_rabbits = 1, > 1, 1 > while True: > yield generation, baby_rabbits > generation += 1 > parent_rabbits, baby_rabbits = \ > baby_rabbits, parent_rabbits + > baby_rabbits > > for pair in fib(): > if pair[0] > 100: > break > print "Generation %d has %d (baby) rabbits." > % pair >
One goal behind the SimplePrograms page is to give people that are new to Python a *gentle* immersion into Python code. I prefer simple: parent_rabbits, baby_rabbits = (1, 1) while baby_rabbits < 100: print 'This generation has %d rabbits' % baby_rabbits parent_rabbits, baby_rabbits = (baby_rabbits, parent_rabbits + baby_rabbits) Somebody commented in another reply that they'd prefer the variable names "a" and "b," but other than that, I think it's hard to simplify this. The problem of counting rabbits is not sufficiently rich to motivate a solution with generator functions, and "yield" statements are just gonna scare people away from the Python, unless they've had a chance to see simpler idioms first. I do think there's a place on the page for a good generators example, but it needs to solve a sufficiently complex problem that the use of generators actually simplifies the solution. So I'm throwing down the gauntlet--can somebody write a short program (maybe 10 to 20 lines) where you solve a problem more simply than a similar non-generator-using solution would solve it? Maybe something like Eight Queens? -- Steve P.S. FWIW the page does already include examples of generator expressions and the itertools module, but it does not yet show any code that actually implements a generator. I would greatly welcome the addition of a good example. ____________________________________________________________________________________ Yahoo! oneSearch: Finally, mobile search that gives answers, not web links. http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC -- http://mail.python.org/mailman/listinfo/python-list