CS1010S Tutorial 7 PDF
CS1010S Tutorial 7 PDF
def col_sum(m):
return accumulate_n(lambda x,y: x+y, 0, m)
row_sum?
Q2
def transpose(m):
return accumulate_n(lambda x, y: [x] + y, [], m)
def row_sum(m):
return accumulate_n(lambda x,y: x+y, 0, transpose(m))
Q3
Q3
Q3
def most_frequent_letters(sentence):
l_c = letter_count(sentence)
max_count = max(l_c,key = lambda i:i[1])[1]
return list(map(lambda i:i[0],
(filter(lambda x: x[1]==max_count, l_c))))
Q4
Q4
def make_queue(): def dequeue(q):
return [] return q.pop(0) if q else None
while size(queue)>=m:
for i in range(m):
enqueue(queue, dequeue(queue)) #brings first player to back of queue
dequeue(queue) #dequeues the bombed guy permanently
winners = []
while size(queue) > 0:
winners.append(dequeue(queue))
return winners
Extra Question
def append_custom(seq1, seq2):
seq1.append(seq2)
a = [1, 2]
a += [a]
b = a.copy()
a[2] = 0
print(a)
print(b)
Extra Question
a = [1, 2]
a += [a]
[1, 2, […]]
if Python is printing a list and discovers that the list is on the stack, that must mean that this
is a self-referential list, and the list should be abbreviated, to avoid an infinite loop.
b = a.copy()
The difference between shallow and deep copying is only relevant for compound objects
(objects that contain other objects, like lists or class instances): A shallow copy constructs a
new compound object and then (to the extent possible) inserts references into it to the
objects found in the original. i.e. print(b) == [1, 2, a]
a[2] = 0
print(a)
print(b)