-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcs1010s-nov15-template.py
209 lines (161 loc) · 4.29 KB
/
cs1010s-nov15-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
## CS1010S Nov15 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 find_oldest(players):
pass # remove this line
## Question 2B
def avg_age(players):
pass # remove this line
## Question 2C
def average(seq, key):
pass # remove this line
## Question 2D
def bmi(height, weight):
pass # remove this line
def avg_bmi(players):
pass # remove this line
## Question 3A
"""
Answer here.
"""
# My own lazy version of the pre-made functions. Dp not modify :)
class Vote:
def __init__(self, string):
self.vote = string
def make_vote(string):
return Vote(string)
def read_vote(vote):
return vote.vote
def is_vote(obj):
return isinstance(obj,Vote)
## Question 3B
def create_box(candidates):
pass # remove this line
def get_candidates(box):
pass # remove this line
def show_votes(box):
pass # remove this line
def cast_vote(box, vote):
pass # remove this line
## Question 3C
def check_vote(box, vote):
pass # remove this line
## Question 3D
def transfer_votes(box1, *boxes):
pass # remove this line
## Question 3E
def count_votes(box):
pass # remove this line
# Provided for Question 4, modify if necessary
class Food:
def __init__(self, name):
self.name = name
self.eaten = False
class Minion(Food):
def __init__(self, name, num_eyes):
self.name = name
self.num_eyes = num_eyes
def get_name(self):
return self.name
def eat(self, food):
if not food.eaten:
print(self.name + " eats " + food.name)
food.eaten = True
else:
print(food.name + " is already eaten!")
self.say("Matoka!")
def say(self, words):
print(self.name + " says '" + words + "'")
## Question 4A
"""
Answer here and modify the code above if necessary.
"""
## Question 4B
"""
Answer here and modify the code above if necessary.
"""
## Question 4C
class OneEyeMinion(Minion):
pass # remove this line
class TwoEyeMinion(Minion):
pass # remove this line
## Question 4D
"""
Answer here.
"""
def mutate(minion):
pass # remove this line
def cure(minion):
pass # remove this line
## Question 4E
class YellowMinion(Minion):
def __init__(self, name, num_eyes):
super.__init__(name, num_eyes)
self.color = 'yellow'
class PurpleMinion(Minion):
def __init__(self, name, num_eyes):
super.__init__(name, num_eyes)
self.color = 'purple'
"""
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
box1 = create_box(('Lion', 'Mouse'))
box2 = create_box(('Lion', 'Mouse'))
box3 = create_box(('Lion', 'Bear'))
def test_q3():
print('Testing Question 3...')
print('='*20)
cast_vote(box1, make_vote('Lion'))
cast_vote(box1, make_vote('Mouse'))
cast_vote(box2, make_vote('Cat'))
cast_vote(box2, make_vote('Mouse'))
compare(1,get_candidates(box1),('Lion', 'Mouse'))
compare(2,show_votes(box1),('Lion', 'Mouse'))
compare(3,show_votes(box2),('Cat', 'Mouse'))
my_vote = make_vote('Lion')
cast_vote(box2, my_vote)
compare(4,show_votes(box2),('Cat', 'Mouse', 'Lion'))
compare(5,show_votes(box1),('Lion', 'Mouse'))
compare(6,check_vote(box1, my_vote),False)
compare(7,check_vote(box2, my_vote),True)
cast_vote(box3, make_vote('Bear'))
cast_vote(box3, make_vote('Lion'))
transfer_votes(box1, box2, box2, box3)
# ('Cat', 'Mouse', 'Lion', 'Lion', 'Mouse') in PDF
compare(8,show_votes(box1),('Lion', 'Mouse','Cat', 'Mouse', 'Lion'))
compare(9,show_votes(box2),())
compare(10,show_votes(box3),('Bear', 'Lion'))
compare(11,count_votes(box1),{'Mouse': 2, 'Lion': 2, 'Spoilt': 1})
test_q3()