Counting check-in areas
Write code · Medium · §9.2 Walking a grid
A neighborhood map divided into cells of equal size is given as the grid grid. If grid[r][c] is '#', someone checked in on social media in the cell in row r, column c. If it is '.', nobody checked in there.
Check-in cells that touch each other above, below, left, or right belong to the same area. Cells that touch only diagonally do not count as touching. Complete the function solution, which returns the number of areas that the check-in cells form.
Function
solution(grid)
Constraints
- The length of
gridis from 1 to 30. - The strings in
gridall have the same length, which is from 1 to 30. - The strings in
gridconsist only of'#'and'.'.
Examples
| grid | result |
|---|---|
['##...', '#..#.', '...##', '.....', '..#..'] | 3 |
['##.', '#..', '..#'] | 2 |
Starter code
def solution(grid):
answer = 0
return answerSolve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.