Skip to content

Commit aae7627

Browse files
author
Priyal595
committed
Added the doctests - Contributes to #9943
1 parent c79034c commit aae7627

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

matrix/count_islands_in_matrix.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,56 @@
44

55

66
class Matrix: # Public class to implement a graph
7+
"""
8+
This public class represents the 2-Dimensional matrix to count the number of islands.
9+
An island is the connected group of 1s including the top, down, right, left as well as
10+
the diagonal connections.
11+
12+
Example -
13+
14+
>>> matrix1 = Matrix(3, 3, [[1, 1, 0], [0, 1, 0], [1, 0, 1]])
15+
>>> matrix1.count_islands()
16+
1
17+
>>> matrix2 = Matrix(2, 2, [[1, 1], [1, 1]])
18+
>>> matrix2.count_islands()
19+
1
20+
21+
"""
722
def __init__(self, row: int, col: int, graph: list[list[bool]]) -> None:
23+
"""
24+
This initialzes the matrix with the given number of rows, columns and the matrix.
25+
26+
Args:
27+
row (int): number of rows in the matrix
28+
col (int): number of columns in the matrix
29+
graph (list[list[bool]]): 2-D list of 0s and 1s representing the matrix
30+
"""
831
self.ROW = row
932
self.COL = col
1033
self.graph = graph
1134

1235
def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool:
36+
"""
37+
This checks if the current cell can be included in the current island.
38+
39+
Args:
40+
i (int): row index
41+
j (int): column index
42+
visited (list[list[bool]]): 2D list tracking the visited cells
43+
44+
Returns:
45+
bool: True if the cell is valid and part of the island(1 for True and ) for False)
46+
47+
>>> visited = [[False, False], [False, False]]
48+
>>> graph = [[1, 0], [0, 1]]
49+
>>> m = Matrix(2, 2, graph)
50+
>>> m.is_safe(0, 0, visited)
51+
1
52+
53+
>>> m.is_safe(0, 1, visited)
54+
0
55+
56+
"""
1357
return (
1458
0 <= i < self.ROW
1559
and 0 <= j < self.COL
@@ -18,6 +62,22 @@ def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool:
1862
)
1963

2064
def diffs(self, i: int, j: int, visited: list[list[bool]]) -> None:
65+
"""
66+
This is the recursive function to mark all the cells visited which are connected to (i, j) indices.
67+
68+
Args:
69+
i (int): row index
70+
j (int): column index
71+
visited (list[list[bool]]): 2D list tracking the visited cells
72+
73+
>>> visited = [[False, False], [False, False]]
74+
>>> graph = [[1, 1], [0, 1]]
75+
>>> m = Matrix(2, 2, graph)
76+
>>> m.diffs(0, 0, visited)
77+
>>> visited
78+
[[True, True], [False, True]]
79+
80+
"""
2181
# Checking all 8 elements surrounding nth element
2282
row_nbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order
2383
col_nbr = [-1, 0, 1, -1, 1, -1, 0, 1]
@@ -27,6 +87,22 @@ def diffs(self, i: int, j: int, visited: list[list[bool]]) -> None:
2787
self.diffs(i + row_nbr[k], j + col_nbr[k], visited)
2888

2989
def count_islands(self) -> int: # And finally, count all islands.
90+
"""
91+
This counts all the islands in the given matrix.
92+
93+
Returns:
94+
int: the number of islands in the given matrix.
95+
96+
Example -
97+
>>> mat = Matrix(1, 1, [[1]])
98+
>>> mat.count_islands()
99+
1
100+
101+
>>> mat2 = Matrix(2, 2, [[0, 0], [0, 0]])
102+
>>> mat2.count_islands()
103+
0
104+
105+
"""
30106
visited = [[False for j in range(self.COL)] for i in range(self.ROW)]
31107
count = 0
32108
for i in range(self.ROW):

0 commit comments

Comments
 (0)