Skip to content

Commit

Permalink
0-island_perimeter.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambesawi committed Nov 30, 2023
1 parent 544352e commit 319daad
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 0x09-island_perimeter/0-island_perimeter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/python3
"""
A module: defines a function that calculates
the perimeter of an island on a given grid
"""


def island_perimeter(grid):
"""
Returns the perimeter of any notable island in the given grid
"""
length = len(grid)
breath = len(grid[0])
perimeter = 0
for x in range(length):
for y in range(breath):
if grid[x][y] == 1:
perimeter += 4
if (y < breath - 1 and grid[x][y + 1] == 1):
perimeter -= 2
if (x < length - 1 and grid[x + 1][y] == 1):
perimeter -= 2
return perimeter

0 comments on commit 319daad

Please sign in to comment.