#79 · Graphs · Intermediate · 70 XP · about 12 min
A map is a list of strings of equal length, where '#' is land and '.' is water. Land squares that touch up, down, left or right belong to the same island; squares that meet only at a corner do not. Write count_islands(grid) that returns how many islands the map has.
grid = [ '##..#', '##...', '..#..', '...##', ] count_islands(grid) # returns 4 count_islands(['#.', '.#']) # returns 2 count_islands(['...']) # returns 0
The first map has four islands: the 2 by 2 block, the single square at the top right, the square in the middle and the pair at the bottom right. In the second map the two squares meet only at a corner, so they are separate islands.
'#' or '.'.Ready?
Accept the challenge to start writing. A small timer counts the time this page is in view.
Output appears here.