473,386 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Apr 5 '06 #1
3 1222
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

Apr 5 '06 #2
"Azolex" <cr****@des.alpes.ch> wrote in message
news:44**********@news.bluewin.ch...
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.00008461627
dbg> 1.00008461627 1.00004230724
dbg> 1.00004230724 1.0000211534
dbg> 1.0000211534 1.00001057664
dbg> 1.00001057664 1.00000528831
dbg> 1.00000528831 1.00000264415
dbg> 1.00000264415 1.00000132207
dbg> 1.00000132207 1.00000066104
dbg> 1.00000066104 1.00000033052
dbg> 1.00000033052 1.00000016526
dbg> 1.00000016526 1.00000008263
dbg> 1.00000008263 1.00000004131
dbg> 1.00000004131 1.00000002066
dbg> 1.00000002066 1.00000001033
dbg> 1.00000001033 1.00000000516
dbg> 1.00000000516 1.00000000258
dbg> 1.00000000258 1.00000000129
dbg> 1.00000000129 1.00000000065
dbg> 1.00000000065 1.00000000032
dbg> 1.00000000032 1.00000000016
dbg> 1.00000000016 1.00000000008
1.00000000016
Apr 5 '06 #3
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))
Apr 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Dr. Mercury | last post by:
Greetings to all - Someone suggested that Java might be the answer for a little conversion program I need. It's pretty simple, really. Just take a column of numbers: 139 259 433 637
0
by: Steve | last post by:
Hi I would like to write a small app that looks for the challenge from my proxy server and entrs my username & pwd then submits. I have looked into the process object and also tried looping...
8
by: Frank Buss | last post by:
A new challenge: http://www.frank-buss.de/marsrescue/index.html Have fun! Now you can win real prices. -- Frank Buß, fb@frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de
0
by: Richard Jones | last post by:
The date for the second PyWeek challenge has been set: Sunday 26th March to Sunday 2nd April (00:00UTC to 00:00UTC). The PyWeek challenge invites entrants to write a game in one week from...
0
by: richard | last post by:
The date for the second PyWeek challenge has been set: Sunday 26th March to Sunday 2nd April (00:00UTC to 00:00UTC). The PyWeek challenge invites entrants to write a game in one week from...
8
by: Mike S | last post by:
Hi all, I noticed a very slight logic error in the solution to K&R Exercise 1-22 on the the CLC-Wiki, located at http://www.clc-wiki.net/wiki/KR2_Exercise_1-22 The exercise reads as...
19
by: octangle | last post by:
This code is attempting to find records that have a RegJrnID that does not occur more than one time in the table. The reason that I want to find records with non-duplicated RegJrnID values is to...
3
by: Thierry | last post by:
For those interested in <b>programming riddles</b>, I would like to announce a new programming challenge I'm just launching at http://software.challenge.googlepages.com This challenge is in its...
7
by: perryche | last post by:
It is like a mountain to me, but I am sure it is nothing to many of your advanced Access Programmers here. Thanks for advance in helping me out. I need to do a "net send" command to active...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.