Can python find fibonacci series in a single line of code?

Gerson Kurz gerson.kurz at t-online.de
Mon Nov 11 14:39:04 EST 2002


On Mon, 11 Nov 2002 14:26:25 -0500, mwilson at the-wire.com (Mel Wilson)
wrote:

>Well, one-liners clearly are not your forte.
>
>>print reduce (lambda x,y: (x[1],x[0]+x[1], x[2]+[x[1]]), xrange(10), (0,1,[]))[2]
>
>Pretty lame compared to
>
>print reduce (lambda x,y: x + [x[-1]+x[-2]], xrange(10), [1, 1] )
>
>right?

Pretty lame compared to

fibonacci = lambda x:map(lambda o:(map(lambda c:map(lambda
l:o.__setslice__(l[0],l[1],l[2]),([o[2]+3,o[2]+4,[o[0]]],[0,3,[o[1],reduce(lambda
x,o:x+o,o[:2]),o[2]+1]])),range(x)),o)[1],[[1,1,0]+range(x)])[0][3:]

(that should be in one line) which you can use like this:

>>> print fibonacci(10)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]



More information about the Python-list mailing list