-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcs1010x-jun17-template.py
208 lines (172 loc) · 4 KB
/
cs1010x-jun17-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
## CS1010X Jun17 Template
## Question 1A
"""
Answer here.
"""
## Question 1B
"""
Answer here.
"""
## Question 1C
"""
Answer here.
"""
## Question 1D
"""
Answer here.
"""
## Question 1E
"""
Answer here.
"""
## Question 2A
def is_fibonacci(n):
pass # remove this line
## Question 2B
"""
Time :
Space :
"""
## Question 2C
def make_memo_fib():
pass # remove this line
## Question 2D
def make_no_consecutive_filter():
pass # remove this line
## Question 2E
def combine(f1,f2):
pass # remove this line
## Question 2F
"""
Answer here.
"""
## Question 3A
def chain(seq,n):
pass # remove this line
## Question 3B
def count_links(chain):
pass # remove this line
## Question 3C
"""
Answer here.
"""
## Question 3D
def copy_chain(chain):
pass # remove this line
## Question 3E
def find_fault(chain):
pass # remove this line
## Question 4A
def reverse_sort(lst,n):
pass # remove this line
## Question 4B
"""
Time :
Space :
"""
## Question 4C
"""
Answer here.
"""
## Question 4D
def print_AP(start,stop,step):
pass # remove this line
## Question 4E
"""
Answer here.
"""
## 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,4,1,11,6,7,8,23,5,8,13]
def test_q2a():
print('Testing Question 2A...')
print('='*20)
compare('X',list(filter(is_fibonacci,a)),[1, 2, 1, 8, 5, 8, 13])
def test_q2c():
print('Testing Question 2C...')
print('='*20)
compare('X',list(filter(make_memo_fib(),a)),[1, 2, 1, 8, 5, 8, 13])
def test_q2d():
print('Testing Question 2D...')
print('='*20)
a = [1,1,2,1,11,1,2,2,2,6,7,7,8,23,8]
compare('X', list(filter(make_no_consecutive_filter(),a)),[1, 2, 1, 11, 1, 2, 6, 7, 8, 23, 8])
def test_q2e():
print('Testing Question 2E...')
print('='*20)
a = [1,1,2,4,1,11,1,2,2,6,7,7,8,23,5,8,13]
f = combine(lambda x: x%2==0, lambda x: x<6)
compare('X', list(filter(f,a)),[2, 4, 2, 2])
def test_q3a():
print('Testing Question 3A...')
print('='*20)
a = [1,2]
a += [a]
b = [4]
b += [b]
compare(1,chain((1,2),1),a)
compare(2,chain((1,2),3),[1,2,[1,2,a]])
compare(3,chain((4,),4),[4,[4,[4,b]]])
def test_q3b():
print('Testing Question 3B...')
print('='*20)
compare(1,count_links(chain((1,2),1)),1)
compare(2,count_links(chain([3,2],2)),2)
compare(3,count_links(chain((4,),4)),4)
def test_q3d():
print('Testing Question 3D...')
print('='*20)
a = chain((1,2),4)
try:
a[2][2][0] = 5
b = copy_chain(a)
test = [1,2]
test += [test]
compare('X',b,[1,2,[1,2,[5,2,test]]])
except:
print('Error! Complete question 3A first!\n')
def test_q3e():
print('Testing Question 3E...')
print('='*20)
a = chain((1,2),4)
try:
a[2][2][0] = 5
test = [1,2]
test += [test]
compare(1,a,[1,2,[1,2,[5,2,test]]])
compare(2,find_fault(a),5)
except:
print('Error! Complete question 3A first!\n')
def test_q4a():
print('Testing Question 4A...')
print('='*20)
a = [4,12,-1,5,7,2,-5,11,8]
b = [-4,2,-1,5,17,2,-3,11,8]
reverse_sort(a,9)
reverse_sort(b,9)
compare(1,a[:9],[12, 11, 8, 7, 5, 4, 2, -1, -5])
compare(2,b[:9],[17, 11, 8, 5, 2, 2, -1, -3, -4])
def test_q4d():
print('Testing Question 4A...')
print('='*20)
compare(1,print_AP(1,5,1),[1, 2, 3, 4])
compare(2,print_AP(1,9,2),[1, 3, 5, 7])
compare(3,print_AP(2,10,3),[2, 5, 8])
compare(4,print_AP(3,16,4),[3,7,11,15])
test_q2a()
test_q2c()
test_q2d()
test_q2e()
test_q3a()
test_q3b()
test_q3d()
test_q3e()
test_q4a()
test_q4d()