1
Hatchling0 / 300 XP
0day streak 0stars
Your Python practice
Mock tests Readiness
More
Guest mode · saved on this device.

Neighbours and bounds

The cells next to (r, c) are found by adding offsets. The 4 direct neighbours are:

for dr, dc in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
    nr = r + dr
    nc = c + dc

For 8 neighbours add the diagonals: loop dr and dc over -1, 0, 1 and skip (0, 0). Always say which one the problem means.

At an edge, some neighbours do not exist. Check before you index:

if 0 <= nr < rows and 0 <= nc < cols:

Python will not warn you about -1: grid[-1] is the last row, so a missing check on the low side silently reads the wrong cell. A missing check on the high side raises IndexError.

Watch the visual: the first candidate is outside and gets rejected.

See it run

grid = [[1, 2], [3, 4]]
r = 0
c = 0
for dr, dc in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
    nr = r + dr
    nc = c + dc
    if 0 <= nr < 2 and 0 <= nc < 2:
        print(grid[nr][nc])

We list the neighbours of the corner cell (0, 0).

Practice neighbours and bounds

Exercise 1

A map is a list of equal-length strings: "." is open ground and "#" is rock. A walker stands on open ground at row row, column col (row 0 is the top, column 0 is the left). The string moves holds the letters U (row − 1), D (row + 1), L (column − 1) and R (column + 1).

Return [row, col], the walker's position after all moves.

Rules:

  • Moves are made in order, one letter at a time.
  • A move that would leave the map or step onto rock is skipped: the walker stays where it is, and the next letter is still used.
  • moves may be empty.

Constraints: map up to 50×50; moves up to 1000 letters.

Example: solution(["..#", "..."], 0, 0, "RRDR") returns [1, 2]: R to (0,1); R is rock, skipped; D to (1,1); R to (1,2).

Exercise 2

A seed tray is a grid of 0 (empty cell) and 1 (a seedling is growing there).

Return how many empty cells have at least one seedling directly above, below, left or right of them.

Rules:

  • Only the 4 direct neighbours count; diagonal cells are not neighbours.
  • Cells outside the tray do not exist.
  • Cells that hold a seedling are never counted.

Constraints: 1 ≤ rows, columns ≤ 100.

Example: solution([[1, 0], [0, 0]]) returns 2: the cells right of and below the seedling count; the bottom-right cell only touches the seedling diagonally.

Exercise 3

A crowd map is a grid of whole numbers: crowd[r][c] is how many people stand on that square. For every square, count how many of its surrounding squares hold strictly fewer people than the square itself.

Return a new grid of the same size holding those counts.

Rules:

  • Surrounding means up to 8 squares: above, below, left, right and the 4 diagonals.
  • Squares outside the map do not exist, so edge and corner squares have fewer neighbours.
  • Equal counts are not fewer.
  • Do not change crowd.

Constraints: 1 ≤ rows, columns ≤ 100; 0 ≤ people ≤ 1000.

Example: solution([[3, 1], [2, 3]]) returns [[2, 0], [1, 2]]. The top-left 3 has neighbours 1, 2 and 3, and two of them are smaller.

The explanation and tasks are public. Running this lesson requires an account with course access.

Practice

Loading Python…
More
Loading exercise content…
Visualizer Watch your code run line by line

Run your code, then choose Play to follow its execution.

Memory · names → values
Variables appear here as Python creates them.
Output & plots

One-line daily quest