-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/holabayor/alx-low_level_programming into main
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |