From aae762719659d2b4b5b5097de7ecafe73b599811 Mon Sep 17 00:00:00 2001 From: Priyal595 <231030016@juitsolan.in> Date: Thu, 23 Oct 2025 20:52:20 +0530 Subject: [PATCH 1/2] Added the doctests - Contributes to #9943 --- matrix/count_islands_in_matrix.py | 76 +++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/matrix/count_islands_in_matrix.py b/matrix/count_islands_in_matrix.py index 64c595e8499d..847be67a2f66 100644 --- a/matrix/count_islands_in_matrix.py +++ b/matrix/count_islands_in_matrix.py @@ -4,12 +4,56 @@ class Matrix: # Public class to implement a graph + """ + This public class represents the 2-Dimensional matrix to count the number of islands. + An island is the connected group of 1s including the top, down, right, left as well as + the diagonal connections. + + Example - + + >>> matrix1 = Matrix(3, 3, [[1, 1, 0], [0, 1, 0], [1, 0, 1]]) + >>> matrix1.count_islands() + 1 + >>> matrix2 = Matrix(2, 2, [[1, 1], [1, 1]]) + >>> matrix2.count_islands() + 1 + + """ def __init__(self, row: int, col: int, graph: list[list[bool]]) -> None: + """ + This initialzes the matrix with the given number of rows, columns and the matrix. + + Args: + row (int): number of rows in the matrix + col (int): number of columns in the matrix + graph (list[list[bool]]): 2-D list of 0s and 1s representing the matrix + """ self.ROW = row self.COL = col self.graph = graph def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool: + """ + This checks if the current cell can be included in the current island. + + Args: + i (int): row index + j (int): column index + visited (list[list[bool]]): 2D list tracking the visited cells + + Returns: + bool: True if the cell is valid and part of the island(1 for True and ) for False) + + >>> visited = [[False, False], [False, False]] + >>> graph = [[1, 0], [0, 1]] + >>> m = Matrix(2, 2, graph) + >>> m.is_safe(0, 0, visited) + 1 + + >>> m.is_safe(0, 1, visited) + 0 + + """ return ( 0 <= i < self.ROW and 0 <= j < self.COL @@ -18,6 +62,22 @@ def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool: ) def diffs(self, i: int, j: int, visited: list[list[bool]]) -> None: + """ + This is the recursive function to mark all the cells visited which are connected to (i, j) indices. + + Args: + i (int): row index + j (int): column index + visited (list[list[bool]]): 2D list tracking the visited cells + + >>> visited = [[False, False], [False, False]] + >>> graph = [[1, 1], [0, 1]] + >>> m = Matrix(2, 2, graph) + >>> m.diffs(0, 0, visited) + >>> visited + [[True, True], [False, True]] + + """ # Checking all 8 elements surrounding nth element row_nbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order 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: self.diffs(i + row_nbr[k], j + col_nbr[k], visited) def count_islands(self) -> int: # And finally, count all islands. + """ + This counts all the islands in the given matrix. + + Returns: + int: the number of islands in the given matrix. + + Example - + >>> mat = Matrix(1, 1, [[1]]) + >>> mat.count_islands() + 1 + + >>> mat2 = Matrix(2, 2, [[0, 0], [0, 0]]) + >>> mat2.count_islands() + 0 + + """ visited = [[False for j in range(self.COL)] for i in range(self.ROW)] count = 0 for i in range(self.ROW): From e15cf33a7b29e51fd8102241f434f880aa9d85d9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 23 Oct 2025 15:42:39 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/count_islands_in_matrix.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/matrix/count_islands_in_matrix.py b/matrix/count_islands_in_matrix.py index 847be67a2f66..5e20bef46fba 100644 --- a/matrix/count_islands_in_matrix.py +++ b/matrix/count_islands_in_matrix.py @@ -8,19 +8,20 @@ class Matrix: # Public class to implement a graph This public class represents the 2-Dimensional matrix to count the number of islands. An island is the connected group of 1s including the top, down, right, left as well as the diagonal connections. - - Example - - + + Example - + >>> matrix1 = Matrix(3, 3, [[1, 1, 0], [0, 1, 0], [1, 0, 1]]) >>> matrix1.count_islands() 1 >>> matrix2 = Matrix(2, 2, [[1, 1], [1, 1]]) >>> matrix2.count_islands() 1 - + """ + def __init__(self, row: int, col: int, graph: list[list[bool]]) -> None: - """ + """ This initialzes the matrix with the given number of rows, columns and the matrix. Args: @@ -35,7 +36,7 @@ def __init__(self, row: int, col: int, graph: list[list[bool]]) -> None: def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool: """ This checks if the current cell can be included in the current island. - + Args: i (int): row index j (int): column index @@ -43,16 +44,16 @@ def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool: Returns: bool: True if the cell is valid and part of the island(1 for True and ) for False) - + >>> visited = [[False, False], [False, False]] >>> graph = [[1, 0], [0, 1]] >>> m = Matrix(2, 2, graph) >>> m.is_safe(0, 0, visited) 1 - + >>> m.is_safe(0, 1, visited) 0 - + """ return ( 0 <= i < self.ROW @@ -69,14 +70,14 @@ def diffs(self, i: int, j: int, visited: list[list[bool]]) -> None: i (int): row index j (int): column index visited (list[list[bool]]): 2D list tracking the visited cells - + >>> visited = [[False, False], [False, False]] >>> graph = [[1, 1], [0, 1]] >>> m = Matrix(2, 2, graph) >>> m.diffs(0, 0, visited) >>> visited [[True, True], [False, True]] - + """ # Checking all 8 elements surrounding nth element 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. Returns: int: the number of islands in the given matrix. - + Example - >>> mat = Matrix(1, 1, [[1]]) >>> mat.count_islands() 1 - + >>> mat2 = Matrix(2, 2, [[0, 0], [0, 0]]) >>> mat2.count_islands() 0 - + """ visited = [[False for j in range(self.COL)] for i in range(self.ROW)] count = 0