Image region detection: find the biggest blob. The grid holds 1s and 0s; return the size (cell count) of the largest connected region of 1s.
Write max_region_size(grid) that returns the number of cells in the largest 4-directionally connected group of 1s, or 0 if there are no 1s.
This is Step 3's scan with a twist: instead of just counting regions, measure each one. Flood every unvisited region, count its cells, and track the maximum size seen.
def neighbors(grid, r, c):
"""Return in-bounds 4-directional neighbor coords: up, down, left, right."""
rows = len(grid)
cols = len(grid[0]) if rows else 0
result = []
for nr, nc in ((r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)):
if 0 <= nr < rows and 0 <= nc < cols:
result.append((nr, nc))
return result
def max_region_size(grid):
"""Return the size of the largest connected region of 1s."""
# TODO: flood each unvisited region, count its cells, track the max
pass
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
Image region detection: find the biggest blob. The grid holds 1s and 0s; return the size (cell count) of the largest connected region of 1s.
Write max_region_size(grid) that returns the number of cells in the largest 4-directionally connected group of 1s, or 0 if there are no 1s.
This is Step 3's scan with a twist: instead of just counting regions, measure each one. Flood every unvisited region, count its cells, and track the maximum size seen.
Press Run Tests or ββ© to check your solution.