← Back to challenges

The Islands

PythonHardalgorithmsmath

Instructions

Create a function that takes a matrix as an argument and returns the number of "islands" in the matrix, as well as the quantity of "islands" by type.

  • You are given a matrix of nxm elements.
  • Each element represents either water (0) or land (1).
  • You must determine the total number of "islands" in the matrix (or map, if you want).
  • An island is a formation of one or more 1, connected horizontally or vertically (not diagonally).

Finally, the program must calculate the number of islands by type:

Type of Island | Abbreviation for Output | Number of Lands --- | --- Micro | Mic | 1 | Small | Sml | >1 and <= 3 | Medium | Med | >3 and <=5 | Big | Big | >5 and <=9 | Massive | Msv | >9 |

Examples

islands([
  [1, 0],
  [0, 0],
])
➞
Tot Islands: 1
Mic Islands: 1
Sml Islands: 0
Med Islands: 0
Big Islands: 0
Msv Islands: 0

islands([
  [1, 0, 1, 1],
  [0, 1, 0, 1],
  [0, 1, 0, 0],
  [1, 1, 0, 1],
])
➞
Tot Islands: 4
Mic Islands: 2
Sml Islands: 1
Med Islands: 1
Big Islands: 0
Msv Islands: 0

Notes

N/A

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.