-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcs1010fc-jun14-template.py
198 lines (162 loc) · 4.04 KB
/
cs1010fc-jun14-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
## CS1010FC Jun14 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
"""
Answer here.
"""
## Question 2B
"""
Answer here.
"""
## Question 3A
def mapf(fns, val):
pass # remove this line, uncomment the next line and replace <T1> and <T2>
#return list(map(<T1>,<T2>))
## Question 3B
def mapv(fns, v):
pass # remove this line
## Question 3C
def mapf_oneline(fns, val):
pass # remove this line
## Question 3D
def filterl(fns, lst):
pass # remove this line
## Question 3E
def vectorize(f):
return f # remove this line
filterlv = vectorize(filterl)
## Question 4A
def first_denomination(kinds_of_coins):
pass # remove this line
## Question 4B
# Replace <T3>, <T4> and <T5>
def cc_iter(a,d):
to_do = [[a,d]]
count = 0
while to_do:
a,d = to_do.pop()
if a<0 or d==0:
pass # <T3>
elif a == 0:
pass # <T4>
else:
pass # <T5>
return count
## Question 4C
def trace(f):
def helper(*args):
count = [0]
if args[0] == "count":
return count[0]
else:
count[0] += 1
return f(*args)
return helper
"""
Answer here.
"""
## Question 4D
"""
Answer here.
"""
## Question 4E
"""
Answer here.
"""
## Question 5A
def fib(a,b,c,d,n):
pass # remove this line
## Question 5B
"""
Time :
Space :
"""
## Question 5C
"""
Answer here.
"""
## 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 count_change(amount):
return cc(amount,5)
## 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_q3a():
print('Testing Question 3A...')
print('='*20)
compare(1,mapf([lambda x: x*2, lambda x: x*x], 4),[8, 16])
compare(2,mapf([lambda x: x*2, lambda x: x*x, lambda x: x/3], 3),[6, 9, 1.0])
def test_q3b():
print('Testing Question 3B...')
print('='*20)
compare(1,mapv([lambda x: x*2, lambda x: x*x], [4]),[[8, 16]])
compare(2,mapv([lambda x: x*2, lambda x: x*x], [4, 2]),[[8, 16], [4, 4]])
compare(3,mapv([lambda x: x, lambda x: x*x, lambda x: x**3], [1,2,3]), [[1, 1, 1], [2, 4, 8], [3, 9, 27]])
def test_q3c():
print('Testing Question 3C...')
print('='*20)
compare(1,mapf_oneline([lambda x: x*2, lambda x: x*x], 4),[8, 16])
compare(2,mapf_oneline([lambda x: x*2, lambda x: x*x, lambda x: x/3], 3), [6, 9, 1.0])
def test_q3d():
print('Testing Question 3D...')
print('='*20)
compare(1,filterl(lambda x: x<5,[1,20,2,3,11]),[1, 2, 3])
compare(2,filterl([lambda x: x<5, lambda x: x%2 == 1], [1,20,2,3,11]),[1,3])
compare(3,filterl([lambda x: x<5, lambda x: x%2 == 1, lambda x: x%2 != 1], [1,20,2,3,11]),[])
def test_q3e():
print('Testing Question 3E...')
print('='*20)
compare(1,filterlv(lambda x: x<5,[[1,20,2,3,11]]),[[1, 2, 3]])
compare(2,filterlv(lambda x: x<5,[[1,20,2,3,11],[1,5,8]]),[[1, 2, 3], [1]])
compare(3,filterlv([lambda x: x<5, lambda x: x%2 == 1], [[1,20,2,3,11]]),[[1, 3]])
compare(4,filterlv([lambda x: x<5, lambda x: x%2 == 1, lambda x: x%2 != 1],[[1,20,2,3,11],[1,2]]),[[], []])
def test_q4b():
print('Testing Question 4B...')
print('='*20)
compare("X",cc_iter(100,5),343)
test_q3a()
test_q3b()
test_q3c()
test_q3d()
test_q3e()
test_q4b()