-
Notifications
You must be signed in to change notification settings - Fork 4
/
cs1010s-nov13-template.py
370 lines (302 loc) · 8.12 KB
/
cs1010s-nov13-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
## CS1010S Nov13 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
def f1(seq):
pass # remove this line
## Question 2B
def f2(seq):
pass # remove this line
## Question 2C
def f3(seq):
pass # remove this line
## Question 2D
def f4(seq):
pass # remove this line
## Question 2E
def f5(seq):
pass # remove this line
## Question 3A
def count_change(amount, coins):
pass # remove this line
## Question 3B
def count_change_limited(amount, coins):
pass # remove this line
## Question 3C
"""
Answer here.
"""
## Question 3D
"""
Answer here.
"""
## Question 4A
"""
Answer here.
"""
## Question 4B
class SparseMatrix:
def __init__(self,seq):
self.data = [len(seq),len(seq[0]),[]]
for i in range(len(seq)):
for j in range(len(seq[0])):
if seq[i][j] != 0:
self.data[2].append([i,j,seq[i][j]])
def rows(self): # Returns the number of rows
pass # <T1>
def cols(self): # Returns the number of columns
pass # <T2>
def get(self,x,y):
for record in self.data[2]:
if record[0] == x and record[1] == y:
return record[2]
return 0
def set(self,x,y,val):
for record in self.data[2]:
if record[0] == x and record[1] == y:
record[2]=val
return
self.data[2].append([x,y,val])
def transpose(self):
for record in self.data[2]:
record[0],record[1]=record[1],record[0]
self.data[0],self.data[1]=self.data[1],self.data[0]
def get_data(self): # Returns a list of lists representing the matrix data according to input format.
pass # <T3>
## Question 4C
class DenseMatrix:
def __init__(self,seq):
self.data = ()
for row in seq:
self.data += (tuple(row),)
def rows(self):
return len(self.data)
def cols(self):
return len(self.data[0])
def get(self,x,y):
return self.data[x][y]
def set(self,x,y,val): # Sets the value at (x,y) to val
pass # <T4>
def transpose(self): # transposes this matrix
pass # <T5>
def get_data(self):
return self.data
## Question 5A
"""
Answer here.
"""
## Question 5B
class Matrix:
def __init__(self,seq):
def choose_sparse(seq):
pass # <T6>
if choose_sparse(seq):
self.mat = SparseMatrix(seq)
else:
self.mat = DenseMatrix(seq)
def rows(self):
return self.mat.rows()
def cols(self):
return self.mat.cols()
def get(self,x,y):
return self.mat.get(x,y)
def set(self,x,y,val):
self.mat.set(x,y,val)
def transpose(self):
self.mat.transpose()
def get_data(self):
return self.mat.get_data()
## Question 5C
def convert(self): # swap internal representation
pass # <T7>
## Question 5D
"""
Answer here.
"""
## Appendix
def cc(amount, kinds_of_coins):
if amount == 0:
return 1
elif amount < 0 or kinds_of_coins == 0:
return 0
else:
return cc(amount, kinds_of_coins-1) + cc(amount - first_denomination(kinds_of_coins), kinds_of_coins)
def first_denomination(kinds_of_coins):
if kinds_of_coins == 1:
return 1
elif kinds_of_coins == 2:
return 5
elif kinds_of_coins == 3:
return 10
elif kinds_of_coins == 4:
return 20
elif kinds_of_coins == 5:
return 50
# Dense Matrix
def make_matrix(seq):
mat = []
for row in seq:
mat.append(list(row))
return mat
def rows(mat):
return len(mat)
def cols(mat):
return len(mat[0])
def get(mat,x,y):
return mat[x][y]
def set(mat,x,y,val):
mat[x][y] = val
def transpose(mat):
transposed = []
for i in range(len(mat[0])):
column = []
for j in range(len(mat)):
column.append(mat[j][i])
transposed.append(column)
mat.clear()
for row in transposed:
mat.append(row)
return mat
def print_matrix(mat):
for row in mat:
print(row)
# Sparse Matrix
def make_matrix(seq):
data = []
for i in range(len(seq)):
for j in range(len(seq[0])):
if seq[i][j] != 0:
data.append([i,j,seq[i][j]])
return [len(seq),len(seq[0]),data]
def rows(mat):
return mat[0]
def cols(mat):
return mat[1]
def get(mat,x,y):
for record in mat[2]:
if record[0] == x and record[1] == y:
return record[2]
return 0
def set(mat,x,y,val):
for record in mat[2]:
if record[0] == x and record[1] == y:
record[2]=val
return
mat[2].append([x,y,val])
def transpose(mat):
for record in mat[2]:
record[0],record[1]=record[1],record[0]
mat[0],mat[1]=mat[1],mat[0]
return mat
def print_matrix(mat):
temp = []
zeros = [0]*mat[1]
for row in range(mat[0]):
temp.append(list(zeros))
for record in mat[2]:
temp[record[0]][record[1]] = record[2]
for row in temp:
print(row)
## 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
a = [1,2,3,4,5,6,7,8,9,10]
b = [10,9,8,7,6,5,4,3,2,1]
c = [1,0,1,0,1,0,2,0,2,0]
d = [5,1,1,1,5]
e = [-1,1,-2,2,-1,1]
def test_q2a():
print('Testing Question 2A...')
print('='*20)
compare(1,f1(a),[1, 2, 3, 4, 100, 6, 7, 8, 9, 10])
compare(2,f1(b),[10, 9, 8, 7, 6, 100, 4, 3, 2, 1])
compare(3,f1(c),[1, 0, 1, 0, 1, 0, 2, 0, 2, 0])
compare(4,f1(d),[100, 1, 1, 1, 100])
compare(5,f1(e),[-1, 1, -2, 2, -1, 1])
def test_q2b():
print('Testing Question 2B...')
print('='*20)
compare(1,f2(a),[2, 4, 100, 6, 8, 10])
compare(2,f2(b),[10, 8, 6, 100, 4, 2])
compare(3,f2(c),[0, 0, 0, 2, 0, 2, 0])
compare(4,f2(d),[100, 100])
compare(5,f2(e),[-2, 2])
def test_q2c():
print('Testing Question 2C...')
print('='*20)
compare(1,f3(a),[4, 3, 2, 1])
compare(2,f3(b),[4, 3, 2, 1])
compare(3,f3(c),[2, 2, 1, 1, 1, 0, 0, 0, 0, 0])
compare(4,f3(d),[1, 1, 1])
compare(5,f3(e),[2, 1, 1, -1, -1, -2])
def test_q2d():
print('Testing Question 2D...')
print('='*20)
compare(1,f4(a),[4, 3, 2, 1])
compare(2,f4(b),[4, 3, 2, 1])
compare(3,f4(c),[2, 1, 0])
compare(4,f4(d),[1])
compare(5,f4(e),[2, 1, -1, -2])
def test_q2e():
print('Testing Question 2E...')
print('='*20)
compare(1,f5(a),[1, 3, 5, 7, 9])
compare(2,f5(b),[10, 8, 6, 4, 2])
compare(3,f5(c),[1, 1, 1, 2, 2])
compare(4,f5(d),[5, 1, 5])
compare(5,f5(e),[-1, -2, -1])
def test_q3a():
print('Testing Question 3A...')
print('='*20)
compare(1,count_change(100,[1,5,10,20,50]),343)
compare(2,count_change(10,[1,5]),3)
compare(3,count_change(13,[1]),1)
compare(4,count_change(10,[1,5,10]),4)
compare(5,count_change(1,[5]),0)
compare(6,count_change(20,[1,10]),3)
compare(7,count_change(5,[1,5]),2)
def test_q3b():
print('Testing Question 3B...')
print('='*20)
compare(1,count_change_limited(100,{1:30,5:20,10:5,20:10,50:1}),203)
compare(2,count_change_limited(10,{1:10,5:10}),3)
compare(3,count_change_limited(13,{1:100}),1)
compare(4,count_change_limited(10,{}),0)
compare(5,count_change_limited(10,{1:5,5:1}),1)
compare(6,count_change_limited(10,{1:5,5:2}),2)
compare(7,count_change_limited(10,{1:10,5:3}),3)
test_q2a()
test_q2b()
test_q2c()
test_q2d()
test_q2e()
test_q3a()
test_q3b()