why 'lambda' and 'reduce'?
Manuel Garcia
news at manuelmgarcia.com
Thu Jun 12 13:36:14 EDT 2003
On Thu, 12 Jun 2003 02:21:57 GMT, Manuel Garcia
<news at manuelmgarcia.com> wrote:
>If anyone ever asks for what good are 'lambda' and 'reduce', show them
>this...
Last night I was thinking about this silly code, and figured out a way
to:
1) add just one more character to the progam, so the expression goes
from 298 characters to 299 characters.
2) double the speed
3) most importantly, add another lambda!
print (lambda p:p[0]+'.'+p[1:])(
str((lambda(x,y,t,a):2L*x*x//a)(
(lambda F:(lambda S:reduce(
lambda(x,y,t,a),_:
(lambda x1,y1:(x1,y1,2L*t,
(a-(t*(x1**2-y1**2))//F)))(
(x+y)//2L,S((x*y)//F)),
[0]*13,(F,(F*F)//S(2L*F),2L,F//2L)))(
lambda n:reduce(lambda x,_:(
x-x//2L+(n*F)//(2L*x)),[0]*15,
n//2L)))(10L**(5010))))[:5000])
the output is still perfectly accurate, of course. (Runs pretty fast
in Python 2.3b1, actually)
(btw I don't see why it only 'almost' qualifies for an "obfuscated"
code contest. I wrote it, and I forgot how it worked around the time
I added the third 'lambda' ;-)
Manuel
More information about the Python-list
mailing list