Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

CS1010S Tutorial 7 PDF

This document summarizes Tutorial 7 on list processing and data structures in CS1010S. It covers accumulating values across nested lists using accumulate_n, calculating column and row sums of a matrix, counting letters and words in a sentence using various list processing techniques, implementing a queue data structure, and determining winners in a "bomberman" game using a queue. Examples of list, string, and other operations are provided to solve the practice questions.

Uploaded by

simyanzi1010
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

CS1010S Tutorial 7 PDF

This document summarizes Tutorial 7 on list processing and data structures in CS1010S. It covers accumulating values across nested lists using accumulate_n, calculating column and row sums of a matrix, counting letters and words in a sentence using various list processing techniques, implementing a queue data structure, and determining winners in a "bomberman" game using a queue. Examples of list, string, and other operations are provided to solve the practice questions.

Uploaded by

simyanzi1010
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CS1010S Tutorial 7

Tutorial 7: List Processing and Data Structures


Quick Recap: Linear data structures
Stack
- First In Last Out (FILO)
- Imagine a literal stack. (E.g pringles can)
- Common operations: pop/push/peek
Queue
- First In First Out (FIFO)
- A queue. Like at Macdonalds.
- Common operations: enqueue/dequeue
Q1
Q1
Q1

def accumulate_n(op, init, sequences):


if (not sequences) or (not sequences[0]):
return type(sequences)()
else:
return type(sequences)([accumulate(op, init, [s[0] for s in sequences])]) \
+ accumulate_n(op, init, type(sequences)(s[1:] for s in sequences))
Q2
Q2

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 count_sentence(sentence): def count_sentence(sentence):


letter_count = len(sentence)-1 words = len(sentence)
for w in sentence: letters = sum(len(w) for w in sentence)
letter_count += len(w) return [words, letters + words - 1]
return [len(sentence), letter_count]
Q3
Q3
def letter_count(sentence): def letter_count(sentence):
chars = [] letters = []
for word in sentence: counts = []
chars.extend(word) for word in sentence:
letters = [] for c in word:
while chars: if c in letters:
letter = chars[0] counts[letters.index(c)] += 1
count = list(filter(lambda x: x == letter, chars)) else:
letters.append([letter, len(count)]) letters.append(c)
chars = list(filter(lambda x: x != letter, chars)) counts.append(1)
return letters return [[letters[i], counts[i]] for i in
range(len(letters))]
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

def enqueue(q, item): def size(q):


q.append(item) return len(q)
Q5
Q5
Q5
def who_wins(m, players):
queue = make_queue()
for player in players:
enqueue(queue, player)

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)

Time and space complexity?


Extra Question

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)

You might also like