-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcs1010x-jun18-template.py
242 lines (188 loc) · 4.47 KB
/
cs1010x-jun18-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
## CS1010X Jun18 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.
"""
## ADT for Question 2
def make_node(entry, left, right):
return [entry, left, right]
def entry(tree):
return tree[0]
def get_left(tree):
return tree[1]
def get_right(tree):
return tree[2]
def set_left(tree,x):
tree[1]=x
def set_right(tree,x):
tree[2]=x
## Question 2A
def make_tree(lst):
pass # remove this line
## Question 2B
def insert_tree(tree, e):
pass # remove this line
## Question 2C
"""
Average
Time :
Space :
Worst
Time :
Space :
"""
## Question 2D
# Draw or type here.
## Question 2E
def depth(tree):
pass # remove this line
## Question 2F
def flatten(tree):
pass # remove this line
## Question 2G
"""
Answer here.
"""
## Question 2H
def balance_tree(tree):
pass # remove this line
## Question 2I
"""
Answer here along with the redefined function(s) if any.
"""
## Question 3A
"""
For simplicity, the UndoList supports the following 4 methods:
• append(x) – appends an element x to the end of the list.
• set(n,x) – sets element at index n to x. Throws IndexError if index is out of
bounds.
• undo – reverses (undo) the last mutation (i.e. either append or set operation).
• show – returns a list containing all the elements.
"""
class UndoList:
pass
## Question 3B
"""
Answer here.
"""
## Question 3C
class LimitedUndoList(UndoList):
pass
## Question 4A
def interleave(lst):
pass # remove this line
## Question 4B
# Just like question 4A but in C, so it's redundant in this case.
## Question 4C
"""
Time :
Space :
"""
## 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
t = make_tree([3,2,1,6,12,9,4,10,11])
def test_q2e():
print('Testing Question 2E...')
print('='*20)
compare('X',depth(t),6)
def test_q2f():
print('Testing Question 2F...')
print('='*20)
compare('X',flatten(t),[1, 2, 3, 4, 6, 9, 10, 11, 12])
def test_q2h():
print('Testing Question 2H...')
print('='*20)
t2 = make_tree([3,2,1,6,12,9,4,10,11])
compare(1,depth(t2),6)
balance_tree(t2)
compare(2,depth(t2),4)
def test_q3a():
print('Testing Question 3A...')
print('='*20)
try:
lst = UndoList()
compare(1,lst.show(),[])
lst.append(2)
lst.append(3)
lst.append(4)
compare(2,lst.show(),[2,3,4])
lst.set(1,99)
compare(3,lst.show(),[2,99,4])
lst.undo()
compare(4,lst.show(),[2,3,4])
lst.undo()
compare(5,lst.show(),[2,3])
lst.undo()
compare(6,lst.show(),[2])
lst.undo()
compare(7,lst.show(),[])
except:
print('Error occured. Define all methods properly.\n')
def test_q3c():
print('Testing Question 3C...')
print('='*20)
try:
lst = LimitedUndoList(3)
compare(1,lst.show(),[])
lst.append(2)
lst.append(3)
lst.append(4)
lst.append(5)
lst.append(6)
compare(2,lst.show(),[2,3,4,5,6])
lst.set(2,99)
compare(3,lst.show(),[2,3,99,5,6])
lst.undo()
compare(4,lst.show(),[2,3,4,5,6])
lst.undo()
compare(5,lst.show(),[2,3,4,5])
lst.undo()
compare(6,lst.show(),[2,3,4])
lst.undo()
compare(7,lst.show(),[2,3,4])
except:
print('Error occured. Define all methods properly.\n')
def test_q4a():
print('Testing Question 4A...')
print('='*20)
a = list(range(10))
a2 = list(range(12))
compare(0.1,a,[0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
compare(0.2,a2,[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
interleave(a)
interleave(a2)
compare(1,a,[0, 5, 1, 6, 2, 7, 3, 8, 4, 9])
compare(2,a2,[0, 6, 1, 7, 2, 8, 3, 9, 4, 10, 5, 11])
test_q2e()
test_q2f()
test_q2h()
test_q3a()
test_q3c()
test_q4a()