Q1a:
for i in range(100, 201, 2):
print(i)
or
for i in range(100, 201):
if i % 2 == 0:
print(i)
-2 marks for not including 200
No penalty for not including 100
-5 marks for printing both odds and evens
-8 marks if some progress was made, but not enough for 5/10
Q1b:
def last_ind(L, e):
for i in range(len(L)-1, -1, -1):
if L[i] == e:
return i
return None
-2 marks for not correctly returning None
-2 marks for not correctly handling len(L)-1 and -1
-8 marks for returning the first index of e instead of the last index
Q2:
def add_sparse_matrices(A, B, dim):
res = []
for i in range(dim[0]):
res.append([0] * dim[1])
for coords, value in A.items():
res[coords[0]][coords[1]] += value
for coords, value in B.items():
res[coords[0]][coords[1]] += value
return res
no marks off for res = [[0]*dim[1]] * dim[0], but let them know that creates a list of aliased lists
-3 marks for not setting up the result matrix correctly
-5 marks for some, but not enough, progress on constructing the sum
Q3
def lcm(a, b, c, d):
for n in range(1, a * b * c * d + 1):
if n % a == 0 and n % b == 0 and n % c == 0 and n % d == 0:
return n
lcm(a, b, c, d) # not necessary
* Don't need to set up a function, but need either a return or a break if the computation stops
4 pts for the idea of going through all the possibilities
2 pts for the exactly correct upper and lower bound
2 pts for the idea of returning/breaking early
2 pts for putting everything together correctly
Q4
prev_args = []
def unlock(input):
prev_args.append(input)
if prev_args[-4:] == ["fall", "costumes", "costumes", "pumpkin"]:
return "unlocked"
else:
return "locked"
3 pts for the idea of storing previous arguments as a global variable
4 pts for setting up the comparison of the last for attempts to the code sequence
3 pts for putting everything together
Q5
====
Only allow 0 and 3
Q5(a)
5
ERROR
Q5(b)
3
None # don't allow "ERROR"
Q5(c)
[[9, 6], [7, 8]]
[[9, 6], [7, 8]]
Q5(d)
[[5, [6, 4]]]
[[5, [6, 4]]]