The Islands

Published by javierdariomartin in

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 IslandAbbreviation for OutputNumber of Lands
MicroMic1
SmallSml>1 and <= 3
MediumMed>3 and <=5
BigBig>5 and <=9
MassiveMsv>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

Watch a quick demo on how Edabit works.