[triangle-zpug] Python challenge

Adam Hupp hupp at upl.cs.wisc.edu
Thu Jun 2 03:45:33 CEST 2005


(Resending from correct address)

On Wed, Jun 01, 2005 at 12:23:44PM -0500, Adam Hupp wrote:
> On Wed, Jun 01, 2005 at 11:28:03AM -0400, Chris Calloway wrote:
> > 
> > 4) Why can't the fibonacci problem be solved easily with just a list 
> > comprehension? (Hint: irrational numbers, recursion, state machines, 
> > computability.) Try to solve the fibonacci series *exactly* (i.e., no 
> > floating point) as a list comprehension *without* a generator or other 
> > user defined function. (You can skip this one if you get stuck. It is 
> > exceedingly difficult. I will buy lunch for the first local TriZPUGer 
> > who can perform this stunt and post it into the public domain on this 
> > list.) Is it possible using Pascal's Triangle?
> > 
> > http://goldennumber.net/pascal.htm

I think this is undocumented behavior but the following works:

>>> [0,1,1] + [dict(enumerate(locals()['_[1]'].__self__)).get(i-1,1) +
    dict(enumerate(locals()['_[1]'].__self__)).get(i-2,1)
    for i in xrange(10)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]

This is based on this recipe:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297

For python 2.4, get rid of the __self__ references.

I'll let someone who figures out the intended solution get the free
lunch :)

-Adam



More information about the triangle-zpug mailing list