[prog] some more python questions

Guru - haunter03 at hotmail.com
Sat Feb 8 16:38:44 EST 2003


The solutions probably aren't that hard but I can't think of any way to 
solve these problems.
The idea is that you have created a class time. all the class contains is a 
'pass'. We are using this class to store the time and then performing 
operations to the time. The examples are being used from How to think like a 
computer scientist -- the python version.

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.

As a second exercise, rewrite increment as a pure function, and write 
function calls to both versions.
I don't know how to do this either...

here is the orginal function:
def increment(time, seconds):
  time.seconds = time.seconds + seconds

  if time.seconds >= 60:
    time.seconds = time.seconds - 60
    time.minutes = time.minutes + 1

  if time.minutes >= 60:
    time.minutes = time.minutes - 60
    time.hours = time.hours + 1

Please offer some help if you can. It's best if you can give me some hints 
so I can work it out..




_________________________________________________________________
Hotmail now available on Australian mobile phones. Go to  
http://ninemsn.com.au/mobilecentral




More information about the Programming mailing list