Check your understanding of the previous reading: nested indexing, the types of a nested list and its parts, and nested loop patterns.

Question 1

Captain the Cat has created a treasure map using the following code:

sea = [
    ["🌊","🌊","🌊","🌊","🌊","🌊"],
    ["🌊","🌊","🌊","🌊","🌊","🌊"],
    ["🌊","🌊","🌊","🌴","💎","🌊"],
    ["🌊","🌊","🌊","🌊","🌊","🌊"],
    ["🌊","🌊","🌊","🌊","🌊","🌊"],
    ["🏴‍☠️","🌊","🌊","🌊","🌊","🌊"],
    ["🚢","🐈","🌊","🌊","🌊","🐠"],
]

Write the nested indexing expressions that will produce the following values:

  • "🌴":
  • "💎":
  • "🚢":
  • "🏴‍☠️":
  • "🐠":
Word bank — not all values are used

Question 2

Given the following code...
scores = [ [5, 3, 4, 3],
           [7, 3, 4, 3],
           [5, 2, 3, 2]]

total = 0
for row in scores:
    for score in row:
        total = total + score
What is the most accurate type for each of the following expressions?
scores
scores[0]
scores[0][0]
row
score
total

Question 3

Given the following code...

scores = [ [5, 3, 4, 3],
           [7, 3, 4, 3],
           [5, 2, 3, 2] ]
  • How many lists are there?
  • How many integers are there?
  • How many elements are in the scores list?
  • How many integers are in the first inner list?
Word bank — not all values are used

Question 4

Given the following code...

def beta(values: list[int]) -> int:
    result = values[0]
    for value in values:
        if value > result:
            result = value
    return result

def alpha(values: list[list[int]]) -> int:
    result = beta(values[0])
    for row in values:
        beta_row = beta(row)
        if beta_row < result:
            result = beta_row
    return result

grades = [
    [90, 99, 95],
    [75, 50, 60],
    [80, 90, 100]
]

What value will be produced when the expression alpha(grades) is evaluated?

Show worked solution

beta is the Max pattern: it returns the highest value of one row. alpha is the Min pattern over the rows, comparing each row's beta result: the row maximums are 99, 75 and 100, and the smallest of those is 75.

Question 5

Given the following code:

def foo(values: list[list[str]]) -> list[str]:
    result = []
    for row in values:
        result.append(bar(row))
    return result

def bar(row: list[str]) -> str:
    result = ""
    for word in row:
        if word and word[-1] == "?":
            result = word
    return result

words = [
    ["Yes", "What?", "No"],
    ["Huh?", "Why?", "How?"],
    ["Oops", "All", "Gone"]
]

What value will be produced when the expression foo(words) is evaluated?

Select one answer, then check.

  • Not quite. Those are the first words of the rows; bar keeps the last word that ends with ?.
  • Not quite. That is the first row itself; foo appends one result from bar for every row.
  • Not quite. That is the second row itself; foo appends one result from bar for every row.
  • Not quite. bar overwrites result at every match, so it keeps the last question in the second row, not the first.
  • Correct. bar is a Find pattern that keeps the last word ending with ?, or "" when there is none: "What?" for the first row, "How?" for the second, and "" for the third. foo maps bar over the rows.
  • Not quite. foo appends one result for every row, so the list has three elements.
  • Not quite. The first two rows each contain words that end with ?, so bar finds one there.