Skip to main content

Module 2 · after

Data structures

The same 6 questions you saw before the module. No running the code: predict, then compare with your first score.

1. What does this print?
t = ['.', 'z', 'o', 'e', '.']
pairs = list(zip(t, t[1:]))
print(len(pairs), pairs[-1])
2. What does this print?
docs = ['bob', 'abby']
uchars = sorted(set(''.join(docs)))
BOS = len(uchars)
print(uchars, BOS)
3. What does this print?
row = []
m = []
for r in range(3):
    row.append(r)
    m.append(row)
print(m[0])
4. What does this print?
class Score:
    def __init__(self, points):
        self.points = points

    def __add__(self, other):
        return Score(self.points + other.points)

a = Score(3)
b = Score(4)
c = a + b + Score(10)
print(c.points, a.points)
5. needs = {'tea': ['water', 'kettle'], 'water': [], 'kettle': ['plug'], 'plug': []}. Which arrow is in this graph?
6. stoi = {'a': 0, 'b': 1, 'c': 2} turns letters into ids. Which loop builds itos = {0: 'a', 1: 'b', 2: 'c'}, turning ids back into letters? (itos starts as {}.)

Your answers are saved with a random id, not your name, so we can see which lessons work. Privacy