Predict the output
What does this print?
grid = ["##.", "#..", "..#"]
land = {(r, c) for r, row in enumerate(grid)
for c, ch in enumerate(row) if ch == "#"}
print(len(land), (2, 2) in land, (1, 1) in land)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 4 True False
Right: four land cells; (2, 2) is land and (1, 1) is water.
Three of the four land cells touch each other; the corner cell touches none of them, so the map has two patches.
Why not the others?
- B 3 True False. Count again: the top-left patch has three cells plus the corner cell.
- C 4 False True. Row 2 is '..#', so column 2 is land.
Steps come from running the Python solution.
Text version
Two patches:
Grid (✕ = blocked): □□✕ / □✕✕ / ✕✕□.
- Visit (0, 0), frontier 1. New patch 1 starts at row 0, column 0
- Visit (1, 0), (0, 1), frontier 2. Patch 1: 2 more land cells next to row 0, column 0
- Visit (2, 2), frontier 1. New patch 2 starts at row 2, column 2
Result: 2
Ring of land:
Grid (✕ = blocked): □□□ / □✕□ / □□□.
- Visit (0, 0), frontier 1. New patch 1 starts at row 0, column 0
- Visit (1, 0), (0, 1), frontier 2. Patch 1: 2 more land cells next to row 0, column 0
- Visit (0, 2), frontier 2. Patch 1: 1 more land cell next to row 0, column 1
- Visit (1, 2), frontier 2. Patch 1: 1 more land cell next to row 0, column 2
- Visit (2, 2), frontier 2. Patch 1: 1 more land cell next to row 1, column 2
- Visit (2, 1), frontier 2. Patch 1: 1 more land cell next to row 2, column 2
- Visit (2, 0), frontier 2. Patch 1: 1 more land cell next to row 2, column 1
Result: 1