[prog] some more python questions
Mary
mary-linuxchix at puzzling.org
Sat Feb 8 16:48:14 EST 2003
On Sat, Feb 08, 2003, Guru - wrote:
> class time:
> pass
>
> 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
>
>
> This function is now correct, but it is not the most efficient solution.
>
> As an exercise, rewrite this function so that it doesn't contain any loops.
> I've really got no idea of how not to use loops to solve the problem.
Have a look at
> while time.seconds > = 60:
> time.seconds = time.seconds - 60
> time.minutes = time.minutes + 1
Imagine what happens when the number of seconds is 182. I subtract 60 3
times, giving me 3 minutes and that leaves 2 seconds.
Repeated subtractions can be replaced with division...
-Mary
More information about the Programming
mailing list