-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample33.py
More file actions
232 lines (199 loc) · 5.19 KB
/
example33.py
File metadata and controls
232 lines (199 loc) · 5.19 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
class Clock:
def __init__(self, h=0, m=0):
'The constructor'
try:
h = int(h)
m = int(m)
except ValueError:
raise ValueError("Error: Hours and minutes should be integers or strings containing integers!")
if h < 0 or h > 23:
raise ValueError("Hour must be between 0 and 23")
if m < 0 or m > 59:
raise ValueError("Minute must be between 0 and 59")
self._hour = h
self._minute = m
def __str__(self):
'convert the time into a printable format (e.g. 23:10)'
return "%02d:%02d" % (self._hour,self._minute)
def __repr__(self):
'Same as __str__'
return str(self)
def tick(self):
'Add one minute to the current time'
self._minute += 1
if self._minute == 60:
self._minute = 0
self._hour = (self._hour + 1) % 24
def __eq__(self, other):
'check whether the current time is equal to the time on the other Clock'
if isinstance(other, Clock):
return self._hour == other._hour and self._minute == other._minute
else:
return False
class PrecisionClock(Clock):
def __init__(self, h=0, m=0, s=0):
super().__init__(h, m)
try:
s=int(s)
except ValueError:
raise ValueError("Error: Seconds should be integers or strings containing integers!")
if s < 0 or s > 59:
raise ValueError("Second must be between 0 and 59")
self._second = s
def __str__(self):
'convert the time into a printable format (e.g. 23:10:05)'
return super().__str__() + ":%02d" % (self._second)
def tick(self):
'Add one second to the current time'
self._second += 1
if self._second == 60:
self._second = 0
super().tick()
def __eq__(self, other):
'check whether the current time is equal to the time on the other Clock'
if super().__eq__(other):
if isinstance(other, PrecisionClock):
return self._second == other._second
return True
return False
# Run the test harness provided for precision clock
#TEST 1
if str(PrecisionClock(23,32)) == "23:32:00":
print("Test 1 PASSED")
else:
print("Test 1 FAILED")
#TEST 2
tmp = PrecisionClock(23,30,23)
[tmp.tick() for i in range(10)]
if str(tmp) == "23:30:33":
print("Test 2 PASSED")
else:
print("Test 2 FAILED")
#TEST 3
tmp = PrecisionClock(23,32)
tmp.tick()
if str(tmp) == "23:32:01":
print("Test 3 PASSED")
else:
print("Test 3 FAILED")
#TEST 4
tmp = PrecisionClock(0,59,59)
tmp.tick()
if str(tmp) == "01:00:00":
print("Test 4 PASSED")
else:
print("Test 4 FAILED")
#TEST 5
tmp = PrecisionClock(12,59,59)
tmp.tick()
if str(tmp) == "13:00:00":
print("Test 5 PASSED")
else:
print("Test 5 FAILED")
#TEST 6
if PrecisionClock(15,2.0,"32") == PrecisionClock(15,"2",32):
print("Test 6 PASSED")
else:
print("Test 6 FAILED")
#TEST 7
if PrecisionClock(15,3,"32") != PrecisionClock(15,2,32):
print("Test 7 PASSED")
else:
print("Test 7 FAILED")
#TEST 8
if PrecisionClock(22,23,0) != PrecisionClock(22,23,1):
print("Test 8 PASSED")
else:
print("Test 8 FAILED")
#TEST 9
if Clock(15,2) == PrecisionClock(15,"2",32):
print("Test 9 PASSED")
else:
print("Test 9 FAILED")
#TEST 10
if PrecisionClock(18,24,32) == Clock(18,24):
print("Test 10 PASSED")
else:
print("Test 10 FAILED")
#TEST 11
if Clock(18,56) != PrecisionClock(18,53,32):
print("Test 11 PASSED")
else:
print("Test 11 FAILED")
#TEST 12
if PrecisionClock(22,44,13) != Clock(23,44):
print("Test 12 PASSED")
else:
print("Test 12 FAILED")
#TEST 13
try:
tmp = PrecisionClock(-1,23,1)
print("Test 13 FAILED")
except ValueError:
print("Test 13 PASSED")
except:
print("Test 13 FAILED")
#TEST 14
try:
tmp = PrecisionClock(12,-23,1)
print("Test 14 FAILED")
except ValueError:
print("Test 14 PASSED")
except:
print("Test 14 FAILED")
#TEST 15
try:
tmp = PrecisionClock(12,23,-1)
print("Test 15 FAILED")
except ValueError:
print("Test 15 PASSED")
except:
print("Test 15 FAILED")
#TEST 16
try:
tmp = PrecisionClock("dsaf",23,1)
print("Test 16 FAILED")
except ValueError:
print("Test 16 PASSED")
except:
print("Test 16 FAILED")
#TEST 17
try:
tmp = PrecisionClock(12,"riidid",1)
print("Test 17 FAILED")
except ValueError:
print("Test 17 PASSED")
except:
print("Test 17 FAILED")
#TEST 18
try:
tmp = PrecisionClock(12,23,"sdcs")
print("Test 18 FAILED")
except ValueError:
print("Test 18 PASSED")
except:
print("Test 18 FAILED")
#TEST 19
try:
tmp = PrecisionClock(12,23,75)
print("Test 19 FAILED")
except ValueError:
print("Test 19 PASSED")
except:
print("Test 19 FAILED")
#TEST 20
try:
tmp = PrecisionClock(12,62,34)
print("Test 20 FAILED")
except ValueError:
print("Test 20 PASSED")
except:
print("Test 20 FAILED")
#TEST 21
try:
tmp = PrecisionClock(26,23,5)
print("Test 21 FAILED")
except ValueError:
print("Test 21 PASSED")
except:
print("Test 21 FAILED")