[Tutor] Re: Comparing Time.

Prahlad Vaidyanathan slime@vsnl.net
Tue, 30 Jul 2002 09:45:27 +0530


Hi,

On Tue, 30 Jul 2002 Troels Leth Petersen spewed into the ether:
> Hi,
> 
> I am reading 'How to Think Like A Computer Scientist' - and have done
> the following exercise:
> 
> "As a second exercise, write a boolean function after that takes two
> Time objects, t1 and t2, as arguments, and returns true (1) if t1
> follows t2 chronologically and false (0) otherwise. "
> 
> And the Time object is a very simple object with 3 attributes: hours,
> minutes, seconds. As said I did the above exercise but in a
> non-elegant way using 9 if statements (3 * if >, <, ==).
> 
> Now I would like to see the Über-cool and short way to do it :)

    Why not do this :

"""
def compare (t1, t2) :
    t1_time = ((t1.hours*3600) + (t1.minutes*60) + t1.seconds)
    t2_time = ((t2.hours*3600) + (t2.minutes*60) + t2.seconds)
    return t1_time < t2_time
"""

HTH,

pv.
-- 
Prahlad Vaidyanathan  <http://www.symonds.net/~prahladv/>

As in certain cults it is possible to kill a process if you know its true name.
		-- Ken Thompson and Dennis M. Ritchie