-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcs1010s-nov20-template.py
270 lines (228 loc) · 6.86 KB
/
cs1010s-nov20-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
## CS1010S Nov20 Template
## Question 1A
"""
Answer here.
"""
## Question 1B
"""
Answer here.
"""
## Question 1C
"""
Answer here.
"""
## Question 1D
"""
Answer here.
"""
## Question 1E
"""
Answer here.
"""
## Provided for Question 2
levels = ((8, 0, 19), (7, 20, 44), (6, 45, 64), (5, 65, 74),
(4, 75, 79), (3, 80, 84), (2, 85, 89), (1, 90, 100))
updated_levels = ((8, 0), (7, 20), (6, 45), (5, 65), (4, 75), (3, 80), (2, 85),
(1, 90))
## Question 2A
def get_level(score, levels):
pass # remove this line
## Question 2B
def create_levels(levels):
return str # placeholder, please remove this line
## Question 2C
def tabulate_score(scores, levels):
pass # remove this line
## Question 2D
def score_distribution(table):
pass # remove this line
## Provided for Question 3
trie = {'a': {'.': ''},
't': {'o': {'.': ''},
'e': {'.': '',
'a': {'.': ''},
'n': {'.': ''}}},
'b': {'o': {'o': {'.': ''}}}}
## Question 3A
def next_char(node, prefix):
pass # remove this line
## Question 3B
def prefix_of(node, char):
pass # remove this line
## Question 3C
def has_word(trie, word):
pass # remove this line
## Question 3D
def add_word(trie, word):
pass # remove this line
## Question 3E
"""
has_word
Time :
Space :
add_word
Time :
Space :
"""
## Provided for Question 4, no need to modify.
class Task:
def __init__(self, name):
self.name = name
self.done = False
class Crewmate:
def __init__(self, tasks):
self.tasks = tasks
self.alive = True
def do(self, task):
if task.done:
print(task.name + ' already done')
else:
print('Doing ' + task.name)
task.done = True
## Question 4A
"""
Answer here.
"""
## Question 4B
"""
Answer here.
"""
## Question 4C
class Impostor: # note Impostor is not a subclass of Crewmate
def kill(self, crewmate):
if crewmate.alive:
print("Stab Stab Stab")
crewmate.alive = False
return True
else:
return False
"""
Answer here and modify the code above.
"""
## Question 4D
class Vigilante: # add the superclasses in the correct order
pass # remove this line
## 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,get_level(20,levels),7)
compare(2,get_level(50,levels),6)
compare(3,get_level(99,levels),1)
def test_q2b():
print('Testing Question 2B...')
print('='*20)
new_psle = create_levels(updated_levels)
compare(1,new_psle(44.5),7)
compare(2,new_psle(64.7),6)
compare(3,new_psle(99.5),1)
def test_q2c():
print('Testing Question 2C...')
print('='*20)
scores = (13, 100, 55, 31, 79, 12, 77, 66, 34, 76, 98)
scores2 = (30, 25, 35, 100, 98, 97)
compare(1,tabulate_score(scores, updated_levels),{8: 2, 1: 2, 6: 1, 7: 2, 4: 3, 5: 1})
compare(2,tabulate_score(scores2, updated_levels),{7: 3, 1: 3})
def test_q2d():
print('Testing Question 2D...')
print('='*20)
scores = (13, 100, 55, 31, 79, 12, 77, 66, 34, 76, 98)
scores2 = (30, 25, 35, 100, 98, 97)
compare(1,score_distribution(tabulate_score(scores, updated_levels)),((4, 3), (1, 2), (7, 2), (8, 2), (5, 1), (6, 1)))
compare(2,score_distribution(tabulate_score(scores2, updated_levels)),((1, 3), (7, 3)))
def test_q3a():
print('Testing Question 3A...')
print('='*20)
try:
compare(1,sorted(next_char(trie, 't')),sorted(['e', 'o']))
except:
compare(1,'error',sorted(['e', 'o']))
compare(2,next_char(trie, 'a'),['.'])
compare(3,next_char(trie, 'e'),[])
try:
compare(4,sorted(next_char(trie['t'], 'e')),sorted(['.', 'a', 'n']))
except:
compare(4,'error',sorted(['.', 'a', 'n']))
def test_q3b():
print('Testing Question 3B...')
print('='*20)
try:
compare(1,sorted(prefix_of(trie, 'o')),sorted(['b', 't']))
except:
compare(1,'error',sorted(['b', 't']))
compare(2,prefix_of(trie, 'a'),[])
try:
compare(3,sorted(prefix_of(trie['t']['e'], '.')),sorted(['a', 'n']))
except:
compare(3,'error',sorted(['a', 'n']))
def test_q3c():
print('Testing Question 3C...')
print('='*20)
compare(1,has_word(trie, 'tea'),True)
compare(2,has_word(trie, 'team'),False)
def test_q3d():
print('Testing Question 3D...')
print('='*20)
add_word(trie, 'are')
add_word(trie, 'bo')
compare('new trie',trie,{'a': {'.': '',
'r': {'e': {'.': ''}}},
't': {'o': {'.': ''},
'e': {'.': '',
'a': {'.': ''},
'n': {'.': ''}}},
'b': {'o': {'.': '',
'o': {'.': ''}}}})
def test_q4():
print('Testing Question 4...')
print('='*20)
tasks = [Task('fix wiring'), Task('empty chute')]
russell = Crewmate(tasks.copy())
tasks.append(Task('swipe card'))
matthew = Crewmate(tasks.copy())
print('# Question 4A #')
russell.do(Task('fix wiring'))
russell.do(Task('fix wiring'))
print()
print('# Question 4B #')
russell.do(russell.tasks[0])
matthew.do(matthew.tasks[0])
print()
print('# Question 4C #')
ben = Impostor()
compare('if Prof Ben "spares" me',ben.kill(russell),True)
compare('if Prof Ben overkills me',ben.kill(russell),False)
jon = Impostor()
try:
compare('if Prof Ben can kill Jon',ben.kill(jon),False)
except Exception as e:
print(e)
print('Modify the code to return False when an Impostor is not killing a Crewmate!\n')
finally:
print('# Question 4D #')
try:
waikay = Vigilante(tasks, matthew)
compare('if Prof Wai Kay wants to kill me too',waikay.kill(russell),False)
compare('if I am not the only one dead',waikay.kill(matthew),True)
compare('if waikay can kill ben',waikay.kill(ben),False)
compare('if the revenge is a success',ben.kill(waikay),True)
except Exception as e:
print(e)
print('Please implement Vigilante correctly!\n')
test_q2a()
test_q2b()
test_q2c()
test_q2d()
test_q3a()
test_q3b()
test_q3c()
test_q3d()
test_q4()