1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
{
"metadata": {
"name": "ch9"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": "CHAPTER 9. : MORE STRUCTURES"
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "example 9.1 page no :90\n"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''\nexample 9.1 page no :90\n'''\n\nclass Time:\n def __init__(self,h,m,s):\n self.hour = h\n self.minute = m\n self.second = s\n\ndef printTime(t):\n print t.hour , \":\" , t.minute , \":\" , t.second\n print \"Time is \" , t.hour , \" hour \" , t.minute , \" minutes \" , t.second \n\nt = Time( 11, 59, 3.14159 )\nprintTime(t)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "11 : 59 : 3.14159\nTime is 11 hour 59 minutes 3.14159\n"
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "example 9.2 page no : 91\n"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''\nexample 9.2 page no : 91\n'''\n\nclass Time:\n def __init__(self,h,m,s):\n self.hour = h\n self.minute = m\n self.second = s\n\ndef printTime(t):\n print t.hour , \":\" , t.minute , \":\" , t.second\n print \"Time is \" , t.hour , \" hour \" , t.minute , \" minutes \" , t.second \n\n\ndef after(time1,time2):\n if (time1.hour > time2.hour):\n return True\n if (time1.hour < time2.hour): \n return False;\n if (time1.minute > time2.minute):\n return True\n if (time1.minute < time2.minute): \n return False;\n if (time1.second > time2.second): \n return True\n return False;\n\nt1 = Time( 11, 59, 3.14159 )\nt2 = Time( 12, 42, 3.234 )\nprint after(t1,t2)\n\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "False\n"
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "example 9.3 page no : 91\n"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''\nexample 9.3 page no : 91\nincorrect version\n'''\nclass Time:\n def __init__(self,h,m,s):\n self.hour = h\n self.minute = m\n self.second = s\n\ndef printTime(t):\n print t.hour , \":\" , t.minute , \":\" , t.second\n print \"Time is \" , t.hour , \" hour \" , t.minute , \" minutes \" , t.second \n\n\n\ndef addTime(t1,t2):\n hour = t1.hour + t2.hour;\n minute = t1.minute + t2.minute;\n second = t1.second + t2.second;\n return Time(hour,minute,second)\n\nc = Time( 9, 14, 30.0 )\nb = Time ( 3, 35, 0.0 )\ndoneTime = addTime (c, b);\nprintTime(doneTime)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "12 : 49 : 30.0\nTime is 12 hour 49 minutes 30.0\n"
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "example 9.4 page no : 92\n"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''\nexample 9.4 page no : 92\nincorrect version\n'''\nclass Time:\n def __init__(self,h,m,s):\n self.hour = h\n self.minute = m\n self.second = s\n\ndef printTime(t):\n print t.hour , \":\" , t.minute , \":\" , t.second\n print \"Time is \" , t.hour , \" hour \" , t.minute , \" minutes \" , t.second \n\n\n\ndef addTime(t1,t2):\n hour = t1.hour + t2.hour;\n minute = t1.minute + t2.minute;\n second = t1.second + t2.second;\n if (second >= 60.0):\n second -= 60.0;\n minute += 1\n if (minute >= 60):\n minute -= 60;\n hour += 1;\n return Time(hour,minute,second)\n\n\nc = Time( 9, 14, 30.0 )\nb = Time ( 3, 35, 0.0 )\ndoneTime = addTime (c, b);\nprintTime(doneTime)\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "12 : 49 : 30.0\nTime is 12 hour 49 minutes 30.0\n"
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "example 9.5 page no : 93\n"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''\nexample 9.5 page no : 93\nincorrect version\n'''\nclass Time:\n def __init__(self,h,m,s):\n self.hour = h\n self.minute = m\n self.second = s\n\ndef printTime(t):\n print t.hour , \":\" , t.minute , \":\" , t.second\n print \"Time is \" , t.hour , \" hour \" , t.minute , \" minutes \" , t.second \n\ndef increment (time,secs):\n time.second += secs\n if (time.second >= 60.0):\n time.second -= 60.0;\n time.minute += 1;\n if (time.minute >= 60):\n time.minute -= 60;\n time.hour += 1;\n\n\ndef addTime(t1,t2):\n hour = t1.hour + t2.hour;\n minute = t1.minute + t2.minute;\n second = t1.second + t2.second;\n if (second >= 60.0):\n second -= 60.0;\n minute += 1\n if (minute >= 60):\n minute -= 60;\n hour += 1;\n return Time(hour,minute,second)\n\n\nc = Time( 9, 14, 30.0 )\nb = Time ( 3, 35, 0.0 )\ndoneTime = addTime (c, b);\nprintTime(doneTime)\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "12 : 49 : 30.0\nTime is 12 hour 49 minutes 30.0\n"
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "example 9.6 page no : 94\n"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''\nexample 9.6 page no : 94\n'''\nclass Time:\n def __init__(self,h,m,s):\n self.hour = h\n self.minute = m\n self.second = s\n\ndef printTime(t):\n print t.hour , \":\" , t.minute , \":\" , t.second\n print \"Time is \" , t.hour , \" hour \" , t.minute , \" minutes \" , t.second \n\ndef addTimeFill(t1,t2,s):\n s.hour = t1.hour + t2.hour;\n s.minute = t1.minute + t2.minute;\n s.second = t1.second + t2.second;\n if (s.second >= 60.0):\n s.second -= 60.0;\n s.minute += 1;\n if (s.minute >= 60):\n s.minute -= 60;\n s.hour += 1;\n\nc = Time( 9, 14, 30.0 )\nb = Time ( 3, 35, 0.0 )\ndoneTime = Time(0,0,0)\naddTimeFill (c, b,doneTime);\nprintTime(doneTime)\n\n\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "12 : 49 : 30.0\nTime is 12 hour 49 minutes 30.0\n"
}
],
"prompt_number": 6
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "example 9.7 page no : 97\n"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''\nexample 9.7 page no : 97\n'''\nclass Time:\n def __init__(self,h,m,s):\n self.hour = h\n self.minute = m\n self.second = s\n\ndef printTime(t):\n print t.hour , \":\" , t.minute , \":\" , t.second\n print \"Time is \" , t.hour , \" hour \" , t.minute , \" minutes \" , t.second \n\ndef convertToSeconds (t):\n minutes = t.hour * 60 + t.minute;\n seconds = minutes * 60 + t.second;\n return seconds;\n\ndef makeTime(secs):\n hour = int (secs / 3600.0);\n secs -= hour * 3600.0;\n minute = int (secs / 60.0);\n secs -= minute * 60;\n second = secs;\n return Time(hour,minute,second)\n\ndef addTime (t1,t2):\n seconds = convertToSeconds (t1) + convertToSeconds (t2);\n return makeTime(seconds)\n\n\nc = Time( 9, 14, 30.0 )\nb = Time ( 3, 35, 0.0 )\ndoneTime = addTime (c, b);\nprintTime(doneTime)\n\n\n\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "12 : 49 : 30.0\nTime is 12 hour 49 minutes 30.0\n"
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
|