← Back to course
Python 11-12 / Week 05 / Tuesday
2/6
Week 05 Β· Grids

Tuesday

Walk the grid
// The test field as rows and columns
⏱ about 30 min

Tuesday: Walk the Grid

Comet wants a rock count before Pip rolls out. "There are only a few rocks," she says. "I can see them. Four, I think."

Wren taps the field sketch. "What does the evidence say?" he asks. "I trust a count that checks every cell. How many cells is that?"

Nova projects the field with a small light that sweeps along the top row, then the next. "How can I help?" she asks. "One loop for the rows and one for the columns inside it. Count how often the inner line runs."

Comet laughs. "Fine. Lead programmer, make the computer check every cell."

Row by row

Nested loops walk a grid. The outer loop picks a row, and the inner loop visits every column in that row.

This order, row by row and left to right, is row-major order. The field below is made-up data from the FieldSim story.

# walk.py
FIELD = [
    "..*..#",
    ".#....",
    "...#*.",
    "*.....",
    "..#..*",
]

rows_walked = 0
checks = 0
rocks = 0
for r in range(len(FIELD)):
    rows_walked += 1
    for c in range(len(FIELD[r])):
        checks += 1
        if FIELD[r][c] == "#":
            rocks += 1
            print("rock at", (r, c))
print("Rows walked:", rows_walked)
print("Cells checked:", checks)
print("Rocks:", rocks)
PREDICT WALK.PY
  • Read the question.
  • Tap your answer.
Which rock is printed first?
What does the Cells checked line show?
rock at (0, 5)
rock at (1, 1)
rock at (2, 3)
rock at (4, 2)
Rows walked: 5
Cells checked: 30
Rocks: 4

Count the lines that run

A statement execution count is how many times a line runs. Counting it by tracing lets you compare loops informally.

walk.py counts for us: rows_walked += 1 runs once per row, and checks += 1 runs once per cell.

Line in walk.pyRunsWhy
rows_walked += 15 timesonce for each row
checks += 130 timesonce for each cell
rocks += 14 timesonce for each #
What does the last line of walk.py show?
COUNT THE INNER LINE
  • Read the question.
  • Tap your answer.
How many times does checks += 1 run?

Open cells in each row

# row_counts.py
FIELD = [
    "..*..#",
    ".#....",
    "...#*.",
    "*.....",
    "..#..*",
]

for r in range(len(FIELD)):
    open_cells = 0
    for c in range(len(FIELD[r])):
        if FIELD[r][c] != "#":
            open_cells += 1
    print("Row", r, "open:", open_cells)
TRACE ROW_COUNTS.PY
  • Read the question.
  • Tap your answer.
What does the Row 3 line show?
How many lines does row_counts.py show?
Why does open_cells = 0 sit inside the outer loop?
Row 0 open: 5
Row 1 open: 5
Row 2 open: 5
Row 3 open: 6
Row 4 open: 5
StatementTrue or false?
In walk.py the outer loop walks the rows.?
The inner loop finishes a whole row before the next row starts.?
The line checks += 1 runs 6 times in walk.py.?
Row-major order means column by column, top to bottom.?
WHY THIS EXERCISERows are outside, columns inside, so the walk is row by row and the inner line runs 30 times.
At your computer
1. In your fieldsim folder, type walk.py, save it and run it. Check the seven lines.
2. Change it to count sample spots: "*" instead of "#". Predict how many, then run it.
3. Change range(len(FIELD[r])) to range(7). Run it and read the IndexError calmly: no row has 7 characters. Then change it back.

Every cell, in order, and the counts to prove it. Tomorrow the grid becomes a class in its own file.

← Monday