Skip to content

Commit 5bd6b19

Browse files
Priyal595Priyal595pre-commit-ci[bot]cclauss
authored
Added the doctests - Contributes to #9943 (#13716)
* Added the doctests - Contributes to #9943 * move work to new branch * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Resolved the error Added the doctests - Contributes to #9943 * Added the doctests - Contributes to #9943 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Priyal595 <231030016@juitsolan.in> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent a07725a commit 5bd6b19

2 files changed

Lines changed: 63 additions & 5 deletions

File tree

matrix/count_islands_in_matrix.py

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,46 @@
11
# An island in matrix is a group of linked areas, all having the same value.
22
# This code counts number of islands in a given matrix, with including diagonal
33
# connections.
4-
5-
64
class Matrix: # Public class to implement a graph
5+
"""This public class represents the 2-Dimensional matrix to count
6+
the number of islands.An island is the connected group of 1s,including the top,
7+
down, right, left as well as the diagonal connections.
8+
>>> matrix1 = Matrix(3, 3, [[1, 1, 0], [0, 1, 0], [1, 0, 1]])
9+
>>> matrix1.count_islands()
10+
1
11+
>>> matrix2 = Matrix(2, 2, [[1, 1], [1, 1]])
12+
>>> matrix2.count_islands()
13+
1
14+
"""
15+
716
def __init__(self, row: int, col: int, graph: list[list[bool]]) -> None:
17+
"""Initializes the matrix with the given number of rows, columns and matrix.
18+
Args:
19+
row (int): number of rows in the matrix
20+
col (int): number of columns in the matrix
21+
graph (list[list[bool]]): 2-D list of 0s and 1s representing the matrix
22+
"""
823
self.ROW = row
924
self.COL = col
1025
self.graph = graph
1126

1227
def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool:
28+
"""This checks if the current cell can be included in the current island.
29+
Args:
30+
i (int): row index
31+
j (int): column index
32+
visited (list[list[bool]]): 2D list tracking the visited cells
33+
Returns:
34+
bool: True if the cell is valid and part of the island
35+
(1 for True and ) for False)
36+
>>> visited = [[False, False], [False, False]]
37+
>>> graph = [[1, 0], [0, 1]]
38+
>>> m = Matrix(2, 2, graph)
39+
>>> m.is_safe(0, 0, visited)
40+
1
41+
>>> m.is_safe(0, 1, visited)
42+
0
43+
"""
1344
return (
1445
0 <= i < self.ROW
1546
and 0 <= j < self.COL
@@ -18,6 +49,19 @@ def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool:
1849
)
1950

2051
def diffs(self, i: int, j: int, visited: list[list[bool]]) -> None:
52+
"""This is the recursive function to mark all the cells visited which
53+
are connected to (i, j) indices.
54+
Args:
55+
i (int): row index
56+
j (int): column index
57+
visited (list[list[bool]]): 2D list tracking the visited cells
58+
>>> visited = [[False, False], [False, False]]
59+
>>> graph = [[1, 1], [0, 1]]
60+
>>> m = Matrix(2, 2, graph)
61+
>>> m.diffs(0, 0, visited)
62+
>>> visited
63+
[[True, True], [False, True]]
64+
"""
2165
# Checking all 8 elements surrounding nth element
2266
row_nbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order
2367
col_nbr = [-1, 0, 1, -1, 1, -1, 0, 1]
@@ -27,6 +71,18 @@ def diffs(self, i: int, j: int, visited: list[list[bool]]) -> None:
2771
self.diffs(i + row_nbr[k], j + col_nbr[k], visited)
2872

2973
def count_islands(self) -> int: # And finally, count all islands.
74+
"""
75+
This counts all the islands in the given matrix.
76+
Returns:
77+
int: the number of islands in the given matrix.
78+
Example -
79+
>>> mat = Matrix(1, 1, [[1]])
80+
>>> mat.count_islands()
81+
1
82+
>>> mat2 = Matrix(2, 2, [[0, 0], [0, 0]])
83+
>>> mat2.count_islands()
84+
0
85+
"""
3086
visited = [[False for j in range(self.COL)] for i in range(self.ROW)]
3187
count = 0
3288
for i in range(self.ROW):

matrix/matrix_class.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,11 @@ def cofactors(self) -> Matrix:
204204
return Matrix(
205205
[
206206
[
207-
self.minors().rows[row][column]
208-
if (row + column) % 2 == 0
209-
else self.minors().rows[row][column] * -1
207+
(
208+
self.minors().rows[row][column]
209+
if (row + column) % 2 == 0
210+
else self.minors().rows[row][column] * -1
211+
)
210212
for column in range(self.minors().num_columns)
211213
]
212214
for row in range(self.minors().num_rows)

0 commit comments

Comments
 (0)