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

Skip Montanaro skip at pobox.com
Mon Nov 11 15:04:58 EST 2002


    Mel> 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]

    Mel> Pretty lame compared to

    Mel> print reduce (lambda x,y: x + [x[-1]+x[-2]], xrange(10), [1, 1] )

    Mel> right?

Which demonstrates that perhaps a one-liner isn't the best solution to this
particular problem.  I realize the OP asked for one, but that Python's
syntax makes it difficult to "overload" a single line with lots of content
isn't always such a bad idea. ;-)

-- 
Skip Montanaro - skip at pobox.com
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list