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

Azolex cretin at des.alpes.ch
Wed Apr 5 10:13:14 EDT 2006


Azolex wrote:
> 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

oops, this should read

               bar1,bar = foo(bar1),bar1

sorry

>         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