Can python find fibonacci series in a single line of code?
Ben Wolfson
wolfson at uchicago.edu
Mon Nov 11 15:14:55 EST 2002
On Mon, 11 Nov 2002 14:26:25 +0000, Mel Wilson wrote:
> In article </f9z9ks/KbEa089yn at the-wire.com>,
> mwilson at the-wire.com (Mel Wilson) wrote:
>>In article <fa61a3d8.0211110720.16a645f8 at posting.google.com>,
>>a_human_work at hotmail.com (Pittaya) wrote:
>>> [ ... ] find fibanicci series in
>>>a single line of [Perl] code.
>>>perl -le '$b=1; print $a+=$b while print $b+=$a'
>
>> One-liner contests are noxious, but what the heck; the
>>first 10, governed by the xrange ..
>
> 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?
Yabbut how about this one:
import sys;x=lambda a,b:(sys.stdout.write('%s\n'%a),x(a+b,a));x(0,1)
Though that does depend on the maximum recursion depth. This one doesn't
have that problem, but it pretty wide:
import sys,operator as o;x=lambda a=[1,0]:(sys.stdout.write('%s\n'%a[-1]),o.setslice(a,0,2,[a[1],a[1]+a[0]]));list(iter(x,x))
This one is shorter but uses more memory:
import sys;x=lambda a=[1,0]:(sys.stdout.write('%s\n'%a),a.extend([a[-1],a[-2]+a[-1]]));list(iter(x,x))
--
BTR
BEN WOLFSON HAS RUINED ROCK MUSIC FOR A GENERATION
-- Crgre Jvyyneq
More information about the Python-list
mailing list