7 questions. Covers Flood fill in Python.
Questions 1 to 5 of 7
fill
grid
labels
python def fill(row, col, label): if row < 0 or row >= len(grid): return if col < 0 or col >= len(grid[0]): return if grid[row][col] != 1: return if labels[row][col] != 0: return fill(row - 1, col, label) fill(row + 1, col, label) fill(row, col - 1, label) fill(row, col + 1, label) labels[row][col] = label
>
>=
None
fill_wrong_bound(0, 0)
python def fill_wrong_bound(row, col): if row < 0 or row > len(grid): return if col < 0 or col >= len(grid[0]): return if grid[row][col] != 1: return grid[row][col] = 2 fill_wrong_bound(row + 1, col) fill_wrong_bound(row, col + 1)
grid[2]
fill(row, col, label)
Use the arrows to put the steps in order.
next_label
python grid = [list("#.."), list("##."), list("..#")] def region_size(row, col): if row < 0 or row >= len(grid): return 0 if col < 0 or col >= len(grid[row]): return 0 if grid[row][col] != ".": return 0 grid[row][col] = "#" return ( 1 + region_size(row - 1, col) + region_size(row + 1, col) + region_size(row, col - 1) + region_size(row, col + 1) ) print(f"first: {region_size(0, 1)}") print(f"second: {region_size(0, 1)}") print(f"third: {region_size(2, 0)}")
3 question(s) left on this page.