CHAPTER 9. : MORE STRUCTURES

example 9.1 page no :90

In [1]:
'''
example 9.1 page no :90
'''

class Time:
    def __init__(self,h,m,s):
        self.hour = h
        self.minute = m
        self.second = s

def printTime(t):
    print t.hour , ":" , t.minute , ":" , t.second
    print "Time is " , t.hour , " hour " , t.minute , " minutes " , t.second 

t = Time( 11, 59, 3.14159 )
printTime(t)
11 : 59 : 3.14159
Time is  11  hour  59  minutes  3.14159

example 9.2 page no : 91

In [2]:
'''
example 9.2 page no : 91
'''

class Time:
    def __init__(self,h,m,s):
        self.hour = h
        self.minute = m
        self.second = s

def printTime(t):
    print t.hour , ":" , t.minute , ":" , t.second
    print "Time is " , t.hour , " hour " , t.minute , " minutes " , t.second 


def after(time1,time2):
    if (time1.hour > time2.hour):
        return True
    if (time1.hour < time2.hour): 
        return False;
    if (time1.minute > time2.minute):
        return True
    if (time1.minute < time2.minute): 
        return False;
    if (time1.second > time2.second): 
        return True
    return False;

t1 = Time( 11, 59, 3.14159 )
t2 = Time( 12, 42, 3.234 )
print after(t1,t2)
False

example 9.3 page no : 91

In [3]:
'''
example 9.3 page no : 91
incorrect version
'''
class Time:
    def __init__(self,h,m,s):
        self.hour = h
        self.minute = m
        self.second = s

def printTime(t):
    print t.hour , ":" , t.minute , ":" , t.second
    print "Time is " , t.hour , " hour " , t.minute , " minutes " , t.second 



def addTime(t1,t2):
    hour = t1.hour + t2.hour;
    minute = t1.minute + t2.minute;
    second = t1.second + t2.second;
    return Time(hour,minute,second)

c = Time( 9, 14, 30.0 )
b = Time ( 3, 35, 0.0 )
doneTime = addTime (c, b);
printTime(doneTime)
12 : 49 : 30.0
Time is  12  hour  49  minutes  30.0

example 9.4 page no : 92

In [4]:
'''
example 9.4 page no : 92
incorrect version
'''
class Time:
    def __init__(self,h,m,s):
        self.hour = h
        self.minute = m
        self.second = s

def printTime(t):
    print t.hour , ":" , t.minute , ":" , t.second
    print "Time is " , t.hour , " hour " , t.minute , " minutes " , t.second 



def addTime(t1,t2):
    hour = t1.hour + t2.hour;
    minute = t1.minute + t2.minute;
    second = t1.second + t2.second;
    if (second >= 60.0):
        second -= 60.0;
        minute += 1
    if (minute >= 60):
        minute -= 60;
        hour += 1;
    return Time(hour,minute,second)


c = Time( 9, 14, 30.0 )
b = Time ( 3, 35, 0.0 )
doneTime = addTime (c, b);
printTime(doneTime)
12 : 49 : 30.0
Time is  12  hour  49  minutes  30.0

example 9.5 page no : 93

In [5]:
'''
example 9.5 page no : 93
incorrect version
'''
class Time:
    def __init__(self,h,m,s):
        self.hour = h
        self.minute = m
        self.second = s

def printTime(t):
    print t.hour , ":" , t.minute , ":" , t.second
    print "Time is " , t.hour , " hour " , t.minute , " minutes " , t.second 

def increment (time,secs):
    time.second += secs
    if (time.second >= 60.0):
        time.second -= 60.0;
        time.minute += 1;
    if (time.minute >= 60):
        time.minute -= 60;
        time.hour += 1;


def addTime(t1,t2):
    hour = t1.hour + t2.hour;
    minute = t1.minute + t2.minute;
    second = t1.second + t2.second;
    if (second >= 60.0):
        second -= 60.0;
        minute += 1
    if (minute >= 60):
        minute -= 60;
        hour += 1;
    return Time(hour,minute,second)


c = Time( 9, 14, 30.0 )
b = Time ( 3, 35, 0.0 )
doneTime = addTime (c, b);
printTime(doneTime)
12 : 49 : 30.0
Time is  12  hour  49  minutes  30.0

example 9.6 page no : 94

In [6]:
'''
example 9.6 page no : 94
'''
class Time:
    def __init__(self,h,m,s):
        self.hour = h
        self.minute = m
        self.second = s

def printTime(t):
    print t.hour , ":" , t.minute , ":" , t.second
    print "Time is " , t.hour , " hour " , t.minute , " minutes " , t.second 

def addTimeFill(t1,t2,s):
    s.hour = t1.hour + t2.hour;
    s.minute = t1.minute + t2.minute;
    s.second = t1.second + t2.second;
    if (s.second >= 60.0):
        s.second -= 60.0;
        s.minute += 1;
    if (s.minute >= 60):
        s.minute -= 60;
        s.hour += 1;

c = Time( 9, 14, 30.0 )
b = Time ( 3, 35, 0.0 )
doneTime = Time(0,0,0)
addTimeFill (c, b,doneTime);
printTime(doneTime)
12 : 49 : 30.0
Time is  12  hour  49  minutes  30.0

example 9.7 page no : 97

In [7]:
'''
example 9.7 page no : 97
'''
class Time:
    def __init__(self,h,m,s):
        self.hour = h
        self.minute = m
        self.second = s

def printTime(t):
    print t.hour , ":" , t.minute , ":" , t.second
    print "Time is " , t.hour , " hour " , t.minute , " minutes " , t.second 

def convertToSeconds (t):
    minutes = t.hour * 60 + t.minute;
    seconds = minutes * 60 + t.second;
    return seconds;

def makeTime(secs):
    hour = int (secs / 3600.0);
    secs -= hour * 3600.0;
    minute = int (secs / 60.0);
    secs -= minute * 60;
    second = secs;
    return Time(hour,minute,second)

def addTime (t1,t2):
    seconds = convertToSeconds (t1) + convertToSeconds (t2);
    return makeTime(seconds)


c = Time( 9, 14, 30.0 )
b = Time ( 3, 35, 0.0 )
doneTime = addTime (c, b);
printTime(doneTime)
12 : 49 : 30.0
Time is  12  hour  49  minutes  30.0
In [ ]: