small challenge : limit((x+1)**0.5 for x in itially(2))

Azolex cretin at des.alpes.ch
Wed Apr 5 09:57:39 EDT 2006


generators challenge
--------------------

define "limit" and "itially"

so that

     limit(foo(x) for x in itially(bar))

works out the same as

     limit2(foo,bar)

with

     def limit2(foo,bar) :
         bar1 = foo(bar)
         while bar != bar1 :
             bar1,bar = foo(bar),bar1
         return bar


Note : be careful with your choice of foo and bar, to prevent infinite 
loops when the iterated value won't converge.

To think of it, perhaps "abs(bar-bar1)>epsilon" would be more 
appropriate than "bar != bar1" in the above loop - I can imagine 
roundoff errors leading to tiny oscillations in the least significant 
bits of an otherwise convergent computation.

Best, az



More information about the Python-list mailing list