-
Notifications
You must be signed in to change notification settings - Fork 4
/
cs1010s-apr14-template.py
222 lines (174 loc) · 5.57 KB
/
cs1010s-apr14-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
## CS1010S Apr14 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 check_charges(seq):
pass # remove this line
## Question 2B
def charge_5(seq):
pass # remove this line
## Question 2C
def find_bad_items(seq):
pass # remove this line
## Question 2D
def update_inventory(seq):
pass # remove this line
## Question 2E
"""
(do not uncomment)
<T1> :
"""
## Question 3A
def sort_magical_items(box):
pass # remove this line
# The magical item ADT
def make_magical_item(id, charges):
return (id, charges)
def get_identity(item):
return item[0]
def get_charges(item):
return item[1]
# The inventory_list ADT
# An inventory_list consists of the name of the magical item
# collection (i.e., ‘wands’, ‘spells’, or ‘potions’) and a list of
# magical_items (i.e., the individual wands, spells, or potions)
def make_inventory_list(iname, items):
return [iname, items]
def get_inventory_name(inv):
return inv[0]
def get_inventory_items(inv):
return inv[1]
# All items are "collected" into an inventory_list, so the user does
# not need to know how the inventory list is actually implemented
def collect(items):
return list(items)
# The magic_box ADT
# A magic_box consists of several inventory_lists
def make_magic_box():
return {}
def all_inventory_lists(box):
return list(box.items())
## Question 3B
def add_inventory_item(inv, item):
pass # remove this line
def remove_inventory_item(inv, item):
pass # remove this line
## Question 3C
def add_inventory_list(box,inv):
iname = get_inventory_name(inv)
items = get_inventory_items(inv)
# <T2>
## Question 3D
def sort_magical_items2(box):
pass # remove this line
class Robot:
def __init__(self, name):
self.name = name
def get_name(self):
return self.name
def set_name(self, new_name):
self.name = new_name
class PlayRobot(Robot):
def __init__(self, name, color):
super().__init__(name)
self.color = color
self.fun_factor = 0
def play(self, time):
self.fun_factor += time
print("New fun factor: " + str(self.fun_factor))
class StudyRobot(Robot):
def __init__(self, name):
super().__init__(name)
self.skills = 0
def teach(self, time):
self.skills += time
print("New skills level:" + str(self.skills))
## Question 4A
"""
Answer here.
"""
## Question 4B
class BuddyRobot(PlayRobot, StudyRobot):
pass # remove this line
## Question 4C
def teach(): # add the parameter(s)
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()
# Pro tip : drag all the lines that you want to comment/uncomment and press Alt+3/4
wands = [(1, 1), (2, 2), (3, 3), (4, -1), (5, 5)]
potions = [(1, 1), (2, 1), (3, 1), (4, 1), (5, 5), (6, 6), (7, 7), (8, 8)]
spells = [(1, 5), (2, -1), (3, 5), (4, 0), (5, -1)]
m_box = {'wands': [(1, 1), (2, 2), (3, 3), (4, -1), (5, 5)],
'potions': [(1, 1), (2, 1), (3, 1), (4, 1), (5, 5), \
(6, 6), (7, 7), (8, 8)],
'spells': [(1, 5), (2, -1), (3, 5), (4, 0), (5, -1)]}
def test_q2a():
print('Testing Question 2A...')
print('='*20)
compare('X',check_charges(spells),[5, -1, 5, 0, -1])
def test_q2b():
print('Testing Question 2B...')
print('='*20)
compare('X',charge_5(wands),[6, 2, 3, 4, 5])
def test_q2c():
print('Testing Question 2C...')
print('='*20)
compare('X',find_bad_items(spells),[(2, -1), (5, -1)])
def test_q3a():
print('Testing Question 3A...')
print('='*20)
sort_magical_items(m_box)
compare('sort',m_box,{'wands': [(5, 5), (3, 3), (2, 2), (1, 1), (4, -1)],
'spells': [(1, 5), (3, 5), (4, 0), (2, -1), (5, -1)],
'potions':[(8, 8), (7, 7), (6, 6), (5, 5),
(1, 1), (2, 1), (3, 1), (4, 1)]})
def test_q3d():
print('Testing Question 3D...')
print('='*20)
another_m_box = {'wands': [(1, 1), (2, 2), (3, 3), (4, -1), (5, 5)],
'potions': [(1, 1), (2, 1), (3, 1), (4, 1), (5, 5), \
(6, 6), (7, 7), (8, 8)],
'spells': [(1, 5), (2, -1), (3, 5), (4, 0), (5, -1)]}
new_box = sort_magical_items2(another_m_box)
compare('the original box',another_m_box,{'wands': [(1, 1), (2, 2), (3, 3), (4, -1), (5, 5)],
'potions': [(1, 1), (2, 1), (3, 1), (4, 1), (5, 5), \
(6, 6), (7, 7), (8, 8)],
'spells': [(1, 5), (2, -1), (3, 5), (4, 0), (5, -1)]})
compare('the new box',new_box,{'potions': [(8, 8), (7, 7), (6, 6), (5, 5), (1, 1), (2, 1), (3, 1), (4, 1)],
'wands': [(5, 5), (3, 3), (2, 2), (1, 1), (4, -1)],
'spells': [(1, 5), (3, 5), (4, 0), (2, -1), (5, -1)]})
# Test case for Q2D will reveal the answer so not provided
test_q2a()
test_q2b()
test_q2c()
test_q3a()
test_q3d()