-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsland.py
More file actions
43 lines (35 loc) · 990 Bytes
/
Island.py
File metadata and controls
43 lines (35 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def islandPerimeter(grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
sum = 0
for row, aRow in enumerate(grid):
for col, val in enumerate(aRow):
if val == 1:
# top
if row == 0:
sum = sum + 1
elif grid[row-1][col] != 1:
sum = sum + 1
# right
if col == len(aRow)-1:
sum = sum + 1
elif grid[row][col+1] != 1:
sum = sum +1
# bottom
if row == len(grid)-1:
sum = sum + 1
elif grid[row+1][col] != 1:
sum = sum + 1
# left
if col == 0:
sum = sum + 1
elif grid[row][col-1] != 1:
sum = sum + 1
return sum
grid = [[0,1,0,0],
[1,1,1,0],
[0,1,0,0],
[1,1,0,0]]
print(islandPerimeter(grid))