Re: small challenge : limit((x+1)**0.5 for x in itially(2))
"Azolex" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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 > > 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) ) Prints out: dbg> 1.41421356237 1.189207115 dbg> 1.189207115 1.09050773267 dbg> 1.09050773267 1.04427378243 dbg> 1.04427378243 1.02189714865 dbg> 1.02189714865 1.01088928605 dbg> 1.01088928605 1.00542990111 dbg> 1.00542990111 1.00271127505 dbg> 1.00271127505 1.00135471989 dbg> 1.00135471989 1.00067713069 dbg> 1.00067713069 1.00033850805 dbg> 1.00033850805 1.00016923971 dbg> 1.00016923971 1.8461627 dbg> 1.8461627 1.4230724 dbg> 1.4230724 1.211534 dbg> 1.211534 1.1057664 dbg> 1.1057664 1.0528831 dbg> 1.0528831 1.0264415 dbg> 1.0264415 1.0132207 dbg> 1.0132207 1.0066104 dbg> 1.0066104 1.0033052 dbg> 1.0033052 1.0016526 dbg> 1.0016526 1.0008263 dbg> 1.0008263 1.0004131 dbg> 1.0004131 1.0002066 dbg> 1.0002066 1.0001033 dbg> 1.0001033 1.516 dbg> 1.516 1.258 dbg> 1.258 1.129 dbg> 1.129 1.065 dbg> 1.065 1.032 dbg> 1.032 1.016 dbg> 1.016 1.008 1.016 -- http://mail.python.org/mailman/listinfo/python-list
Re: small challenge : limit((x+1)**0.5 for x in itially(2))
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 -- http://mail.python.org/mailman/listinfo/python-list
small challenge : limit((x+1)**0.5 for x in itially(2))
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 -- http://mail.python.org/mailman/listinfo/python-list