-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcs1010s-apr16-template.py
323 lines (270 loc) · 8.07 KB
/
cs1010s-apr16-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
## CS1010S Apr16 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.
"""
## Question 2A
"""
Queues are supported by the following functions:
• make_queue(): returns an empty queue.
• enqueue(q, item): adds item to the back of queue q.
• dequeue(q): removes the item at the front of the queue q, and returns it. If the queue is
empty, None is returned.
• size(q): returns the number of items in the queue q.
"""
def make_queue():
pass # remove this line
def enqueue(q, item):
pass # remove this line
def dequeue(q):
pass # remove this line
def size(q):
pass # remove this line
## Question 2B
def below_4(p1, p2):
pass # remove this line
## Question 2C
def priority_enqueue(q, fn, p):
pass # remove this line
## Question 2D
"""
Answer here.
"""
## Question 2E
"""
Answer here.
"""
## Question 2F
"""
Answer here.
"""
## Question 3A
def valid_heap(node):
pass # remove this line
## Question 3B
def swap(n1, n2):
pass # remove this line
## Question 3C
def bubble_up(node):
pass # remove this line
## Question 3D
def bubble_down(node):
pass # remove this line
## Question 3E
"""
Answer here.
"""
## Question 3F
"""
Answer here.
"""
## Provided for Question 4
class Jedi:
def __init__(self, name):
self.name = name
self.powers = ['jump', 'heal', 'mind trick', 'push']
def do(self, action):
if action in self.powers:
print(self.name + " performs Force " + action)
else:
print(self.name + " does not know Force " + action)
class Sith:
def __init__(self, name):
self.name = "Darth " + name
self.powers = ['jump', 'lightning', 'choke', 'push']
def do(self, action):
if action in self.powers:
print(self.name + " performs Force " + action)
else:
print(self.name + " does not know Force " + action)
## Question 4A
##maul = Sith("Maul")
##maul.do("lightning")
##
##yoda = Jedi("Yoda")
##yoda.do("choke")
"""
Answer here.
"""
## Question 4B
class JediTurnSith(Jedi,Sith):
def __init__(self,name):
super().__init__(name)
"""
Answer here.
"""
## Question 4C and 4D
class ForceUser:
def __init__(self, name):
self.name = name
self.powers = [] # creates empty list of powers
def do(self, action):
if action in self.powers:
print(self.name + " performs Force " + action)
else:
print(self.name + " does not know Force " + action)
class Jedi(ForceUser):
pass # remove this line
class Sith(ForceUser):
pass # remove this line
class JediTurnSith: # 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()
# Made the lazy version of age and Node to prevent error. Do not modify :)
def age(p):
if p == 'Andy':
return 4
elif p == 'Bob':
return 30
elif p == 'Jack':
return 2
else:
return 18
class Node:
def __init__(self,value):
self.value = value
self.left = None
self.right = None
self.parent = None
one = Node(1)
two = Node(2)
four = Node(4)
five = Node(5)
seven = Node(7)
eleven = Node(11)
sixteen = Node(16)
eighteen = Node(18)
twenty_one = Node(21)
twenty_four = Node(24)
thirty = Node(30)
another_five = Node(5)
another_eleven = Node(11)
another_twenty_four = Node(24)
another_sixty_seven = Node(67)
another_fourty_eight = Node(48)
another_fifty_two = Node(52)
newbie_eight = Node(8)
yet_another_eleven = Node(11)
yet_another_twenty_four = Node(24)
yet_another_sixty_seven = Node(67)
yet_another_fourty_eight = Node(48)
yet_another_fifty_two = Node(52)
yet_another_fifty_five = Node(55)
# Heap 1
## 2
## 4 7
## 18 21
two.left, two.right = four, seven
four.parent, four.left = two, eighteen
seven.parent, seven.left = two, twenty_one
eighteen.parent, twenty_one.parent = four, seven
# Heap 2
## 5
## 11 24
## 30 16 1
five.left, five.right = eleven, twenty_four
eleven.parent, eleven.left, eleven.right = five, thirty, sixteen
twenty_four.parent, twenty_four.left = five, one
thirty.parent, sixteen.parent, one.parent = eleven, eleven, twenty_four
# Heap 3
## 5
## 11 24
## 67 48 52
## 8
another_five.left, another_five.right = another_eleven, another_twenty_four
another_eleven.left, another_eleven.right, another_eleven.parent = another_sixty_seven, another_fourty_eight, another_five
another_twenty_four.parent, another_twenty_four.left = another_five, another_fifty_two
another_sixty_seven.parent, another_fourty_eight.parent, another_fifty_two.parent = another_eleven, another_eleven, another_twenty_four
another_fourty_eight.left, newbie_eight.parent = newbie_eight, another_fourty_eight
# Heap 4
## 55
## 11 24
## 67 48 52
yet_another_fifty_five.left, yet_another_fifty_five.right = yet_another_eleven, yet_another_twenty_four
yet_another_eleven.left, yet_another_eleven.right, yet_another_eleven.parent = yet_another_sixty_seven, yet_another_fourty_eight, yet_another_fifty_five
yet_another_twenty_four.parent, yet_another_twenty_four.left = yet_another_fifty_five, yet_another_fifty_two
yet_another_sixty_seven.parent, yet_another_fourty_eight.parent, yet_another_fifty_two.parent = yet_another_eleven, yet_another_eleven, yet_another_twenty_four
def min_node(n1, n2, n3):
return min(list(filter(lambda x:x != None,[n1,n2,n3])),key=lambda x:x.value)
# Pro tip : drag all the lines that you want to comment/uncomment and press Alt+3/4
def test_q2b():
print('Testing Question 2B...')
print('='*20)
compare(1,below_4('Andy','Bob'),False)
compare(2,below_4('Jack','Bob'),True)
compare(3,below_4('Andy','Jack'),False)
def test_q3():
print('Testing Question 3...')
print('='*20)
compare(1,valid_heap(one),True)
compare(2,valid_heap(two),True)
compare(3,valid_heap(four),True)
compare(4,valid_heap(seven),True)
compare(5,valid_heap(eleven),True)
compare(6,valid_heap(sixteen),True)
compare(7,valid_heap(eighteen),True)
compare(8,valid_heap(twenty_one),True)
compare(9,valid_heap(thirty),True)
compare(10,valid_heap(five),False)
compare(11,valid_heap(twenty_four),False)
swap(five, one) # five is 1 and one is 5
compare(12,eleven.parent.value,1)
compare(13,twenty_four.left.value,5)
swap(five, one) # back to the original heap
compare(14,eleven.parent.value,5)
compare(15,twenty_four.left.value,1)
bubble_up(newbie_eight)
compare(16,another_five.left.value,8)
compare(17,another_sixty_seven.parent.value,8)
compare(18,another_fourty_eight.value,11)
compare(19,newbie_eight.value,48)
compare(20,another_eleven.value,8)
bubble_down(yet_another_fifty_five)
compare(21,yet_another_twenty_four.parent.value,11)
compare(22,yet_another_fifty_five.left.value,48)
compare(23,yet_another_fourty_eight.value,55)
compare(24,yet_another_eleven.right.value,55)
compare(25,yet_another_fourty_eight.parent.value,48)
def test_q4d():
print('Testing Question 4D...')
print('='*20)
try:
emperor = Sith("Sidious", "Palpatine")
compare(1,emperor.name,'Darth Sidious')
compare(2,emperor.alias,'Palpatine')
vader = JediTurnSith("Vader", "Anakin")
compare(3,vader.name,'Darth Vader')
compare(4,vader.alias,'Anakin')
except Exception as e:
print(e)
print('Error occured. Please make sure all methods are well-defined.\n')
test_q2b()
test_q3()
test_q4d()