Skip to content

Commit

Permalink
X
Browse files Browse the repository at this point in the history
  • Loading branch information
holabayor committed Oct 3, 2022
2 parents 8731781 + 5d85915 commit 65754cc
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions 0x1C-makefiles/5-island_perimeter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/python3
"""island_ perimeter module."""


def island_perimeter(grid):
"""Function that calculates the perimeter of an island on a grid."""
i = 0
j = 0
perimeter = 0
if grid is None or type(grid) is not list or type(grid[0]) is not list:
return 0
length = len(grid)
length2 = len(grid[0])
while i < length:
while j < length2:
if grid[i][j] == 1:
if i == 0 or grid[i - 1][j] == 0:
perimeter += 1
if j == 0 or grid[i][j - 1] == 0:
perimeter += 1
if j == length2 - 1 or grid[i][j + 1] == 0:
perimeter += 1
if i == length - 1 or grid[i + 1][j] == 0:
perimeter += 1
j += 1
j = 0
i += 1
return perimeter

0 comments on commit 65754cc

Please sign in to comment.