Skip to content

Commit e15cf33

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent aae7627 commit e15cf33

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

matrix/count_islands_in_matrix.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@ class Matrix: # Public class to implement a graph
88
This public class represents the 2-Dimensional matrix to count the number of islands.
99
An island is the connected group of 1s including the top, down, right, left as well as
1010
the diagonal connections.
11-
12-
Example -
13-
11+
12+
Example -
13+
1414
>>> matrix1 = Matrix(3, 3, [[1, 1, 0], [0, 1, 0], [1, 0, 1]])
1515
>>> matrix1.count_islands()
1616
1
1717
>>> matrix2 = Matrix(2, 2, [[1, 1], [1, 1]])
1818
>>> matrix2.count_islands()
1919
1
20-
20+
2121
"""
22+
2223
def __init__(self, row: int, col: int, graph: list[list[bool]]) -> None:
23-
"""
24+
"""
2425
This initialzes the matrix with the given number of rows, columns and the matrix.
2526
2627
Args:
@@ -35,24 +36,24 @@ def __init__(self, row: int, col: int, graph: list[list[bool]]) -> None:
3536
def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool:
3637
"""
3738
This checks if the current cell can be included in the current island.
38-
39+
3940
Args:
4041
i (int): row index
4142
j (int): column index
4243
visited (list[list[bool]]): 2D list tracking the visited cells
4344
4445
Returns:
4546
bool: True if the cell is valid and part of the island(1 for True and ) for False)
46-
47+
4748
>>> visited = [[False, False], [False, False]]
4849
>>> graph = [[1, 0], [0, 1]]
4950
>>> m = Matrix(2, 2, graph)
5051
>>> m.is_safe(0, 0, visited)
5152
1
52-
53+
5354
>>> m.is_safe(0, 1, visited)
5455
0
55-
56+
5657
"""
5758
return (
5859
0 <= i < self.ROW
@@ -69,14 +70,14 @@ def diffs(self, i: int, j: int, visited: list[list[bool]]) -> None:
6970
i (int): row index
7071
j (int): column index
7172
visited (list[list[bool]]): 2D list tracking the visited cells
72-
73+
7374
>>> visited = [[False, False], [False, False]]
7475
>>> graph = [[1, 1], [0, 1]]
7576
>>> m = Matrix(2, 2, graph)
7677
>>> m.diffs(0, 0, visited)
7778
>>> visited
7879
[[True, True], [False, True]]
79-
80+
8081
"""
8182
# Checking all 8 elements surrounding nth element
8283
row_nbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order
@@ -92,16 +93,16 @@ def count_islands(self) -> int: # And finally, count all islands.
9293
9394
Returns:
9495
int: the number of islands in the given matrix.
95-
96+
9697
Example -
9798
>>> mat = Matrix(1, 1, [[1]])
9899
>>> mat.count_islands()
99100
1
100-
101+
101102
>>> mat2 = Matrix(2, 2, [[0, 0], [0, 0]])
102103
>>> mat2.count_islands()
103104
0
104-
105+
105106
"""
106107
visited = [[False for j in range(self.COL)] for i in range(self.ROW)]
107108
count = 0

0 commit comments

Comments
 (0)