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

David Brown david at no.westcontrol.spam.com
Tue Nov 12 03:27:42 EST 2002


"Carl Banks" <imbosol at vt.edu> wrote in message
news:aqp3b3$jm3$1 at solaris.cc.vt.edu...
>
>
> Because it's fun!!!
>

Fair enough - I too have tried writing illegable python code (see below).  I
remember once having a game for the BBC Micro writen in a single line of
Basic...

>
> while 1: globals().setdefault("a",0); globals().setdefault("b",1); \
>     __import__("sys").stdout.write("%d "% a); a,b=b+a,a
>

I've learned something here - I didn't know you could access globals() in
this way.  (I also didn't know you could do such on-the-fly imports, but
that has less use outside the scope of one-liners).


Here's a challenge for perl programmers - define a function to take a string
and produce a CRC checksum of the string, all in one line:

foobar = lambda ts : reduce(lambda x, y : reduce(lambda (x, s), b : (x, (((s
\
<< 1) & 0xff) + ((x & (1 << b)) > 0)) ^ ((s & 0x80) >> 7) * 0x8d), range(8),
\
(0, x^y))[1], map(ord, [t for t in ts]), 0)


Usage:
    foobar("Hello")







More information about the Python-list mailing list