Problem 268229 · medium · Phase 02 Linear Data Structures

Minesweeper Numbers

2d-lists · neighbours · grid-construction

A minesweeper field is a grid of strings where "*" marks a mine and "." an empty cell. Return a new grid of integers: each mine becomes -1, and each empty cell becomes the number of mines among its eight neighbours.

Examples

Input:  field = [[".", "*"],
                 [".", "."]]
Output: [[1, -1],
         [1, 1]]

Input:  field = [["*", "*"],
                 ["*", "*"]]
Output: [[-1, -1],
         [-1, -1]]

Input:  field = [["."]]
Output: [[0]]

Constraints

  • 1 <= rows, cols <= 100

Goals

  • Count 8-directional neighbours matching a value
  • Produce an integer grid from a character grid
Starting Python…