Start free

Advanced · 2D lists · 183 of 264

Minesweeper numbers

Replace every empty square (.) of a minesweeper field with the number of mines (*) in the 8 squares around it. Mines stay *.

Input: r and c, then r lines of c characters. Output: the field with numbers.

Examples

Input
3 3
*..
...
..*
Output
*10
121
01*
Input
1 1
.
Output
0
Input
2 4
*.*.
.*..
Output
*3*1
2*21

One more test checks other inputs when you press Check.

Stuck? The idea behind 2d lists

A list of lists is a grid: grid[row][col]. Read one row per line, each turned into numbers.

Loop over rows, then over the columns inside each row. zip(*grid) turns rows into columns.

grid = [[1, 2, 3], [4, 5, 6]]
for row in grid:
    print(*row)
print(grid[1][2])  # 6

Watch for: [[0] * 3] * 2 makes two names for the same row: changing one changes both. Build rows with a comprehension: [[0] * 3 for _ in range(2)].

All the 2d lists exercises
Your code
Opening the code editor…
Python 3.14 in your browser
Output
Run shows what your program prints. Check tests it on every example and the hidden tests.

More 2d lists exercises