-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcs1010s-nov22-template.py
450 lines (391 loc) · 12.1 KB
/
cs1010s-nov22-template.py
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
## CS1010S Nov22 Template
## Question 1A
"""
Answer here.
"""
## Question 1B
"""
Answer here.
"""
## Question 1C
"""
Answer here.
"""
## Question 1D
"""
Answer here.
"""
## Question 1E
"""
Answer here.
"""
## Question 1F
"""
Answer here.
"""
## Provided for Question 2
game = [[3, 7, 3, 0], [12, 0, 0, 3], [1, 0, 13, 7], [1, 4, 6]]
game2 = ([3, 7, 3, 0], [12, 0, 0, 3], [1, 0, 13, 7], [1, 4, 6])
## Question 2A
def new_game(num, pits, cnts):
pass # remove this line
## Question 2B
def has_ended(game):
pass # remove this line
## Question 2C
def next_pit(game, player, pit):
pass # remove this line
## Question 2D
def distribute(game, player, pit):
pass # remove this line
## Question 2E
def play_turn(game, player, pit):
pass # remove this line
## Question 2F
"""
Answer here.
"""
## Provided for Question 3
# My own ADT, should be viable
class Product:
def __init__(self, name):
self.name = name
def __hash__(self):
return hash(self.name)
def __eq__(self, other):
return isinstance(other, Product) and self.name == other.name
def __repr__(self):
return f'<Product \'{self.name}\'>'
class Category:
def __init__(self, name):
self.name = name
def __hash__(self):
return hash(self.name)
def __eq__(self, other):
return isinstance(other, Category) and self.name == other.name
def __repr__(self):
return f'<Category \'{self.name}\'>'
def shelf_life(product):
from collections import defaultdict
dd = defaultdict(lambda: 100)
dd.update({7: 10, 9: 90, 8: 70})
return dd[len(product.name)]
def category(product):
from collections import defaultdict
dd = defaultdict(lambda: 'Others')
dd.update({'HL Milk': 'Milk', 'Milo': 'Drink', 'Dutch Lady': 'Milk', 'Milo Plus': 'Drink'})
return Category(dd[product.name])
inventory = {
Product('HL Milk'): {90: 10, 92: 10},
Product('Dutch Lady'): {91: 7, 94: 10},
Product('Milo'): {87: 2, 92: 20}
}
catalog = {
Category('Milk'): {
Product('HL Milk'): {90: 10, 92: 10},
Product('Dutch Lady'): {91: 7, 94: 10}
},
Category('Drink'): {
Product('Milo'): {87: 2, 92: 20}
}
}
## Question 3A
def stock_up(inv, products, today):
pass # remove this line
## Question 3B
def clear_stock(inv, today):
pass # remove this line
## Question 3C
def make_catalog(inv):
pass # remove this line
## Question 3D
def count_stock(inv, cat, thing):
pass # remove this line
## Question 3E
"""
Answer here.
"""
## Provided for Question 4, feel free to modify.
class Ship:
def __init__(self, weight, full):
self.weight = weight
self.full = full
def displacement(self):
return self.weight
class CargoShip(Ship):
def __init__(self, weight, full):
super().__init__(weight, full)
self.cargo = []
def displacement(self):
return sum(map(lambda x: x.weight(), self.cargo)) \
+ super().displacement()
def load(self, item):
self.cargo.append(item)
# Assume this is defined
class Overweight(Exception):
def __str__(self):
return 'Item is too heavy'
# For 4B onwards
class Tanker(CargoShip):
def __init__(self, weight, full):
self.tank = Tank()
self.load(self.tank)
super().__init__(weight, full)
def load(self, amount):
self.tank.add(amount)
## Question 4A
"""
Just modify the classes above.
"""
## Question 4B
"""
Just answer here and modify the classes above if necessary.
"""
## Question 4C
class Tank:
pass
## Question 4D
class PassengerFerry: # fill the superclass if necessary
pass
# DO NOT MODIFY THIS!
class RoPaxFerry(PassengerFerry, CargoShip):
pass
## Test Cases
def compare(number, got, expected):
if got == expected:
print(f'Test {number} : Passed!')
else:
print(f'Test {number} : Failed!')
print(f'Expected {expected}, got {got}')
print()
# Pro tip : drag all the lines that you want to comment/uncomment and press Alt+3/4
def test_q2a():
print('Testing Question 2A...')
print('='*20)
compare(1, new_game(1, 2, 3), [[3, 3], [0]])
compare(2, new_game(4, 1, 10), [[10], [10], [10], [10], [0, 0, 0, 0]])
game = new_game(2, 5, 7)
try:
compare(3, game[0] is game[1], False)
except:
compare(3, True, False)
def test_q2b():
print('Testing Question 2B...')
print('='*20)
game = [[10], [10], [10], [10], [0, 0, 0, 0]]
compare(1, has_ended(game), False)
game = [[1], [1], [1], [1], [0, 0, 0, 0]]
compare(1, has_ended(game), True)
game = [[1, 0], [0, 1], [20, 20]]
compare(3, has_ended(game), True)
game = [[1, 0], [1, 1], [20, 20]]
compare(4, has_ended(game), False)
def test_q2c():
print('Testing Question 2C...')
print('='*20)
game = [[3, 7, 3, 0], [12, 0, 0, 3], [1, 0, 13, 7], [1, 4, 6]]
compare(1, next_pit(game, 0, 3), (1, 0))
compare(2, next_pit(game, 1, 2), (1, 3))
compare(3, next_pit(game, 2, 3), (0, 0))
compare(4, next_pit(game, 0, 1), (0, 2))
compare(5, next_pit(game, 1, 3), (2, 0))
def test_q2d():
print('Testing Question 2D...')
print('='*20)
game = [[3, 7, 3, 0], [12, 0, 0, 3], [1, 0, 13, 7], [1, 4, 6]]
compare(1, distribute(game, 0, 1), (2, 1))
compare(2, game, [[3, 0, 4, 1], [13, 1, 1, 4], [2, 0, 13, 7], [1, 4, 6]])
compare(3, distribute(game, 1, 1), (1, 3))
compare(4, game, [[3, 0, 4, 1], [13, 0, 2, 4], [2, 0, 13, 7], [1, 4, 6]])
def test_q2e():
print('Testing Question 2E...')
print('='*20)
game = [[3, 7, 3, 0], [12, 0, 0, 3], [1, 0, 13, 7], [1, 4, 6]]
play_turn(game, 1, 3)
compare(1, game, [[4, 8, 4, 1], [13, 1, 1, 0], [0, 1, 14, 0], [1, 6, 6]])
play_turn(game, 2, 1)
compare(2, game, [[0, 8, 4, 1], [13, 1, 1, 0], [0, 0, 15, 0], [1, 6, 10]])
def test_q3a():
print('Testing Question 3A...')
print('='*20)
inventory = {
Product('HL Milk'): {90: 10, 92: 10},
Product('Dutch Lady'): {91: 7, 94: 10},
Product('Milo'): {87: 2, 92: 20}
}
inventory2 = {
Product('HL Milk'): {90: 11, 92: 10},
Product('Dutch Lady'): {91: 7, 94: 10},
Product('Milo'): {87: 2, 92: 20}
}
inventory3 = {
Product('HL Milk'): {90: 11, 92: 10, 93: 1},
Product('Dutch Lady'): {91: 7, 93: 1, 94: 10},
Product('Milo'): {87: 2, 92: 20, 93: 1}
}
inventory4 = {
Product('HL Milk'): {90: 11, 92: 11, 93: 1},
Product('Dutch Lady'): {91: 7, 93: 1, 94: 10},
Product('Milo'): {87: 2, 92: 21, 93: 1}
}
inventory5 = {
Product('HL Milk'): {90: 12, 92: 11, 93: 1},
Product('Dutch Lady'): {91: 7, 93: 1, 94: 10},
Product('Milo'): {87: 2, 92: 21, 93: 1},
Product('Milo Plus'): {90: 1}
}
stock_up(inventory, [Product('HL Milk')], 90)
compare(1, inventory, inventory2)
stock_up(inventory, [Product('HL Milk'), Product('Dutch Lady'), Product('Milo')], 93)
compare(2, inventory, inventory3)
stock_up(inventory, [Product('HL Milk'), Product('Milo')], 92)
compare(3, inventory, inventory4)
stock_up(inventory, [Product('Milo Plus'), Product('Milo')], 90)
compare(4, inventory, inventory5)
def test_q3b():
print('Testing Question 3B...')
print('='*20)
inventory = {
Product('HL Milk'): {90: 10, 92: 10},
Product('Dutch Lady'): {91: 7, 94: 10},
Product('Milo'): {87: 2, 92: 20}
}
clear_stock(inventory, 1000)
compare(1, inventory, {
Product('HL Milk'): {},
Product('Dutch Lady'): {},
Product('Milo'): {}
})
inventory = {
Product('HL Milk'): {90: 10, 92: 10},
Product('Dutch Lady'): {91: 7, 94: 10},
Product('Milo'): {87: 2, 92: 20}
}
clear_stock(inventory, 190)
compare(2, inventory, {
Product('HL Milk'): {92: 10},
Product('Dutch Lady'): {91: 7, 94: 10},
Product('Milo'): {92: 20}
})
def test_q3c():
print('Testing Question 3C...')
print('='*20)
inventory = {
Product('HL Milk'): {90: 10, 92: 10},
Product('Dutch Lady'): {91: 7, 94: 10},
Product('Milo'): {87: 2, 92: 20}
}
catalog = {
Category('Milk'): {
Product('HL Milk'): {90: 10, 92: 10},
Product('Dutch Lady'): {91: 7, 94: 10}
},
Category('Drink'): {
Product('Milo'): {87: 2, 92: 20}
}
}
compare(1, make_catalog(inventory), catalog)
inventory = {
Product('HL Milk'): {90: 10, 92: 10},
Product('Dutch Lady'): {91: 7, 94: 10},
Product('Milo'): {87: 2, 92: 20},
}
catalog2 = {
Category('Milk'): {
Product('HL Milk'): {90: 10, 92: 10},
Product('Dutch Lady'): {91: 7, 94: 10}
},
Category('Drink'): {
Product('Milo'): {87: 2, 88: 1, 92: 20},
Product('Milo Plus'): {88: 1}
},
Category('Others'): {
Product('Bear'): {88: 1}
}
}
stock_up(inventory, [Product('Milo Plus'), Product('Milo'), Product('Bear')], 88)
compare(2, make_catalog(inventory), catalog2)
def test_q3d():
print('Testing Question 3D...')
print('='*20)
inventory = {
Product('HL Milk'): {90: 10, 92: 10},
Product('Dutch Lady'): {91: 7, 94: 10},
Product('Milo'): {87: 2, 92: 20}
}
catalog = {
Category('Milk'): {
Product('HL Milk'): {90: 10, 92: 10},
Product('Dutch Lady'): {91: 7, 94: 10}
},
Category('Drink'): {
Product('Milo'): {87: 2, 92: 20}
}
}
catalog2 = {
Category('Milk'): {
Product('HL Milk'): {90: 10, 92: 10},
Product('Dutch Lady'): {91: 7, 94: 10}
},
Category('Drink'): {
Product('Milo'): {87: 2, 88: 1, 92: 20},
Product('Milo Plus'): {88: 1}
},
Category('Others'): {
Product('Bear'): {88: 1}
}
}
compare(1, count_stock(inventory, catalog, Product('Hilo')), 0)
compare(2, count_stock(inventory, catalog, Product('Milo')), 22)
compare(3, count_stock(inventory, catalog, Category('Milk')), 37)
compare(4, count_stock(inventory, catalog2, Product('Hilo')), 0)
compare(5, count_stock(inventory, catalog2, Product('Milo')), 22)
compare(6, count_stock(inventory, catalog2, Category('Drink')), 24)
compare(7, count_stock(inventory, catalog, Category('Others')), 0)
compare(8, count_stock(inventory, catalog2, Category('Others')), 1)
def test_q4c():
print('Testing Question 4C...')
print('='*20)
try:
evercrude = Tanker(1000, 8500)
except:
print('You should implement Tank first!\n')
try: evercrude.load(10000)
except: compare(1, True, False)
else: compare(1, True, True)
try: evercrude.load(10000)
except Overweight: compare(2, True, True)
except: compare(2, True, False)
else: compare(2, True, False)
def test_q4d():
print('Testing Question 4D...')
print('='*20)
try:
car = Tank()
car.add(500/0.75)
compare(1, car.weight(), 500)
except:
compare(1, 'implement Tank first!', True)
try:
everseen = RoPaxFerry(1000, 2000)
everseen.board(500)
except:
print('You should implement Tank first!\n')
try: everseen.load(car)
except: compare(2, True, False)
else: compare(2, True, True)
try: everseen.load(car)
except Overweight: compare(3, True, True)
except: compare(3, True, False)
else: compare(3, True, False)
test_q2a()
test_q2b()
test_q2c()
test_q2d()
test_q2e()
test_q3a()
test_q3b()
test_q3c()
test_q3d()
test_q4c()
test_q4d()