Practice

Problem 9 of 10

Problem · Hard · 35 min

Count the lakes

  • Hard
  • Grids
  • Recursion

A map is a list of strings. All strings have the same length. In each string, "~" is water and "." is land.

A lake is a group of water cells that touch each other. Two cells touch when they are next to each other in the same row, or above each other in the same column. Cells that meet only at a corner do not touch.

Write count_lakes(grid). It returns the number of lakes on the map.

  • An empty map has 0 lakes.

Examples

Call Result
count_lakes(['~~..', '~...', '..~~']) 2
count_lakes(['...', '...']) 0
count_lakes(['~.~', '.~.', '~.~']) 5

Tasks

2 to do

  1. Task 1, not checked yet. Make count_lakes pass the example tests.

  2. Task 2, not checked yet. Make count_lakes pass the hidden tests.

Task 1 of 2

Make count_lakes pass the example tests.

All tasks

main.py

Ctrl+Enter runs your code. Ctrl+Shift+Enter runs all the tests. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.