-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-count-islands.py
113 lines (91 loc) · 2.4 KB
/
03-count-islands.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def print_matrix(matrix):
if matrix == None:
return
for i in range(len(matrix)):
print(" ", end="")
for j in range(len(matrix[i])):
print(matrix[i][j], end="")
print("")
def count_islands(matrix):
# check input
if matrix == None \
or (not isinstance(matrix,list)) \
or (not all(isinstance(ele, list) for ele in matrix)):
return 0
# create new matrix to keep track of visits
visited = [[False for _ in range(len(matrix[0]))] for _ in range(len(matrix))]
num_islands = 0
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if matrix[i][j] == 1 and not visited[i][j]:
dfs(i, j, matrix, visited)
num_islands += 1
return num_islands
def dfs(i, j, matrix, visited):
if i in range(len(matrix)) and j in range(len(matrix[i])) \
and matrix[i][j] == 1 \
and visited[i][j] == False:
visited[i][j] = True
dfs(i-1, j, matrix, visited) # visit north
dfs(i+1, j, matrix, visited) # visit south
dfs(i, j+1, matrix, visited) # visit east
dfs(i, j-1, matrix, visited) # visit west
# Clean Sample Matrix
matrix1 = [
[1,1,0,0,0],
[0,1,0,0,1],
[1,0,0,1,1],
[1,0,1,0,1]
]
print_matrix(matrix1)
print("Matrix has: {} islands (expect 4)".format(count_islands(matrix1)))
# PR Example Matrix
matrix2 = [
[1,1,0,0,0],
[0,1,0,0,1],
[1,0,0,1,1],
[0,0,0,0,0],
[1,0,1,0,1]
]
print_matrix(matrix2)
print("Matrix has: {} islands (expect 6)".format(count_islands(matrix2)))
# Empty Column
matrix3 = [
[],
[],
[],
[],
[]
]
print_matrix(matrix3)
print("Matrix has: {} islands (expect 0)".format(count_islands(matrix3)))
# single column
matrix4 = [
[1],
[1],
[0],
[1],
[1]
]
print_matrix(matrix4)
print("Matrix has: {} islands (expect 2)".format(count_islands(matrix4)))
# Single Row
matrix5 = [
[1, 0, 1, 1, 0, 1]
]
print_matrix(matrix5)
print("Matrix has: {} islands (expect 3)".format(count_islands(matrix5)))
# Null matrix
matrix6 = None
print_matrix(matrix6)
print("Matrix has: {} islands (expect 0)".format(count_islands(matrix6)))
# Malformed Matrix
matrix7 = [
[1,1,0,0,0],
[0,1],
[1,0,0,1,1],
[0,0,1],
[1,0,1,0,1]
]
print_matrix(matrix7)
print("Matrix has: {} islands (expect 6)".format(count_islands(matrix7)))