small challenge : fixpoint((x+1)**0.5 for x in itially(2))
Azolex
cretin at des.alpes.ch
Wed Apr 5 18:28:02 EDT 2006
Paul McGuire wrote:
> Howzis?
>
> -- Paul
>
>
> class Bag:
> pass
> data = Bag()
> data.x = None
>
> def itially(bar):
> if data.x is None:
> data.x = bar
> while 1:
> yield data.x
>
> def limit(z):
> eps = 1e-10
> done = False
> z2 = z.next()
> z1 = z2 + 1
> while abs(z2-z1) > eps:
> data.x = z2
> z2, z1 = z.next(),z2
> print "dbg>",z1,z2
> return z1
>
> print limit( x**0.5 for x in itially(2) )
It fits the bill, I'd say. Below is my simplest solution, renaming
"limit" to a more appropriate "fixpoint". Like yours, this solution as
concurrency issues.
Note that (z+1)**0.5 has a more interesting attractive fixpoint that
z**0.5, namely the golden mean 1.618...
def itially(z) :
echoback.z = z
while True :
yield echoback.z
def echoback(gen) :
while True :
echoback.z = gen.next()
yield echoback.z
def fixpoint(gen) :
z = echoback(gen).next
while True :
if z()==z() :
return z()
print fixpoint((z+1)**0.5 for z in itially(2))
More information about the Python-list
mailing list