2/32
Part 1 of 7 · A grid is a graphLearn and do

A grid is a graph

The infection moves from a cell to the cells that share a side with it. So the first tool lists a cell's neighbors:

# up, down, left, right
SIDES = ((-1, 0), (1, 0), (0, -1), (0, 1))


def neighbors(grid, r, c):
    found = []
    for dr, dc in SIDES:
        nr, nc = r + dr, c + dc
        if 0 <= nr < len(grid) and 0 <= nc < len(grid[0]):
            found.append((nr, nc))
    return found


grid = ["....",
        "....",
        "...."]
for cell in [(0, 0), (0, 1), (1, 1), (2, 3)]:
    print(cell, neighbors(grid, *cell))
Predict

On an open 3 by 4 grid, how many neighbors does the corner (0, 0) have?

Pick one to see what it prints.
Pick an answer above to see the rest