Re: [Tutor] no loops
> def increment(time, seconds): > time.seconds = time.seconds + seconds > > while time.seconds >= 60: >time.seconds = time.seconds - 60 >time.minutes = time.minutes + 1 Tale a look at what this loop is doing. Think about its purpose. If you werre doing this with paper and pencil would you really use iteration? Think division > As an exercise, rewrite this function so that it > doesn't contain any loops. > > I have been staring at this function and drawing a > blank. Something tells me that I need to use > iteration, but I am not sure how I could implement it. The loops are implementing a mathematical function which doesn't need a loop. Look at the division and modulo operators. Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] no loops
Christopher Spears wrote: > I am working on another problem from "How To Think > Like A Computer Scientist". Here is a function: > > def increment(time, seconds): > time.seconds = time.seconds + seconds > > while time.seconds >= 60: > time.seconds = time.seconds - 60 > time.minutes = time.minutes + 1 > > while time.minutes >= 60: > time.minutes = time.minutes - 60 > time.hours = time.hours + 1 > > Here is the function in action: > > from time import * atime = Time() atime.hours = 1 atime.minutes = 60 atime.seconds = 120 printTime(atime) > 1:60:120 > increment(atime,1) printTime(atime) > 2:2:1 > > Now the exercise is: > As an exercise, rewrite this function so that it > doesn't contain any loops. > > I have been staring at this function and drawing a > blank. Something tells me that I need to use > iteration, but I am not sure how I could implement it. > take a look at the divmod built-in function. > -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] no loops
--- John Fouhy <[EMAIL PROTECTED]> wrote: > On 12/07/06, Christopher Spears > <[EMAIL PROTECTED]> wrote: > > Now the exercise is: > > As an exercise, rewrite this function so that it > > doesn't contain any loops. > > > > I have been staring at this function and drawing a > > blank. Something tells me that I need to use > > iteration, but I am not sure how I could implement > it. > > Hi Chris, > > You are using iteration. That's what loops are :-) > > Perhaps you meant to say "recursion", which is where > a function calls > itself. You could solve this recursively, but I > think Gregor's > comment is closer to what they want you to do. > > -- > John. I agree with Gregor and John. What makes the problem difficult is the fact that time is represented using 3 different units of measure: hours, minutes, and seconds. The math becomes much simpler if you convert the time value to a single unit (such as seconds). But it doesn't have to be seconds. I recall seeing one database that stores time as fractional hours where a minute is worth 1/60 of an hour and a second is worth 1/3600 of an hour. In other words, 1:15 is stored as 1.25 hours, 4:30 is stored as 4.5 hours, and so forth. Converting from (hours, minutes, seconds) to fractional hours is pretty easy, but going the other way is not so simple. __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] no loops
On 12/07/06, Christopher Spears <[EMAIL PROTECTED]> wrote: > Now the exercise is: > As an exercise, rewrite this function so that it > doesn't contain any loops. > > I have been staring at this function and drawing a > blank. Something tells me that I need to use > iteration, but I am not sure how I could implement it. Hi Chris, You are using iteration. That's what loops are :-) Perhaps you meant to say "recursion", which is where a function calls itself. You could solve this recursively, but I think Gregor's comment is closer to what they want you to do. -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] no loops
Christopher Spears schrieb: IF you know that it's 2 seconds after midnight, how many hours, minutes, seconds after midnight ist this. If you should compute this by hand, how would you proceed? Best wishes, Gregor > I am working on another problem from "How To Think > Like A Computer Scientist". Here is a function: > > def increment(time, seconds): > time.seconds = time.seconds + seconds > > while time.seconds >= 60: > time.seconds = time.seconds - 60 > time.minutes = time.minutes + 1 > > while time.minutes >= 60: > time.minutes = time.minutes - 60 > time.hours = time.hours + 1 > > Here is the function in action: > > from time import * atime = Time() atime.hours = 1 atime.minutes = 60 atime.seconds = 120 printTime(atime) > 1:60:120 > increment(atime,1) printTime(atime) > 2:2:1 > > Now the exercise is: > As an exercise, rewrite this function so that it > doesn't contain any loops. > > I have been staring at this function and drawing a > blank. Something tells me that I need to use > iteration, but I am not sure how I could implement it. > > -Chris > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor