From 214e18a13bc3b14101287621f8dc62f015989911 Mon Sep 17 00:00:00 2001 From: CheerfulBear22 <.> Date: Mon, 3 Feb 2025 19:48:07 +0000 Subject: [PATCH 1/5] Added doctests for matrix/count_islands_in_matrix.py --- matrix/count_islands_in_matrix.py | 46 +++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/matrix/count_islands_in_matrix.py b/matrix/count_islands_in_matrix.py index 64c595e84..8d53bfd7e 100644 --- a/matrix/count_islands_in_matrix.py +++ b/matrix/count_islands_in_matrix.py @@ -5,11 +5,36 @@ class Matrix: # Public class to implement a graph def __init__(self, row: int, col: int, graph: list[list[bool]]) -> None: + """ + Initialise matrix with number of rows, columns, and graph + + >>> m = Matrix(4, 3, [[True, False, False, False], [True, False, True, False], [False, False, True, True]]) + >>> m.ROW + 4 + >>> m.COL + 3 + >>> m.graph + [[True, False, False, False], [True, False, True, False], [False, False, True, True]] + """ self.ROW = row self.COL = col self.graph = graph def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool: + """ + Checks if the cell (i, j) can be visited + + >>> visited = [[False, False, False], [False, False, False], [False, False, False]] + >>> m = Matrix(3, 3, [[True, False, False], [False, False, True], [False, False, True]]) + >>> m.is_safe(0, 0, visited) + True + >>> m.is_safe(0, 2, visited) + False + >>> m.is_safe(-1, 2, visited) + False + >>> m.is_safe(1, 5, visited) + False + """ return ( 0 <= i < self.ROW and 0 <= j < self.COL @@ -18,7 +43,15 @@ class Matrix: # Public class to implement a graph ) def diffs(self, i: int, j: int, visited: list[list[bool]]) -> None: - # Checking all 8 elements surrounding nth element + """ + Checking all 8 elements surrounding nth element + + >>> visited = [[False, False, False], [False, False, False], [False, False, False]] + >>> m = Matrix(3, 3, [[True, True, False], [False, True, False], [True, False, True]]) + >>> m.diffs(0, 0, visited) + >>> visited + [[True, True, False], [False, True, False], [False, False, False]] + """ row_nbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order col_nbr = [-1, 0, 1, -1, 1, -1, 0, 1] visited[i][j] = True # Make those cells visited @@ -26,7 +59,16 @@ class Matrix: # Public class to implement a graph if self.is_safe(i + row_nbr[k], j + col_nbr[k], visited): self.diffs(i + row_nbr[k], j + col_nbr[k], visited) - def count_islands(self) -> int: # And finally, count all islands. + def count_islands(self) -> int: + """ + Counts the number of islands in the matrix + + >>> m = Matrix(3, 4, [[True, True, False, False], [False, True, False, True], [True, False, False, True]]) + >>> m.count_islands() + 3 + >>> m2 = Matrix(3, 3, [[True, True, False], [True, False, False], [False, False, True]]) + 2 + """ visited = [[False for j in range(self.COL)] for i in range(self.ROW)] count = 0 for i in range(self.ROW): From 16bb4b50e42fe3b0ab90a15f6e7046243a69cc9a Mon Sep 17 00:00:00 2001 From: CheerfulBear22 <.> Date: Tue, 4 Feb 2025 19:20:43 +0000 Subject: [PATCH 2/5] Changed matrix initialisation in count_islands_in_matrix.py. Addressing requested changes on #12555 --- matrix/count_islands_in_matrix.py | 73 ++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/matrix/count_islands_in_matrix.py b/matrix/count_islands_in_matrix.py index 64c595e84..393b4c53f 100644 --- a/matrix/count_islands_in_matrix.py +++ b/matrix/count_islands_in_matrix.py @@ -4,12 +4,45 @@ class Matrix: # Public class to implement a graph - def __init__(self, row: int, col: int, graph: list[list[bool]]) -> None: - self.ROW = row - self.COL = col + def __init__(self, graph: list[list[bool]]) -> None: + """ + Initialise matrix with number of rows, columns, and graph. + + >>> m = Matrix([[True, False, False, False], + ... [True, False, True, False], + ... [False, False, True, True]]) + >>> m.ROW + 3 + >>> m.COL + 4 + >>> m.graph + [[True, False, False, False], + [True, False, True, False], + [False, False, True, True]] + """ self.graph = graph + self.ROW = len(graph) + self.COL = len(graph[0]) def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool: + """ + Checks if the cell (i, j) can be visited. + + >>> visited = [[False, False, False], + ... [False, False, False], + ... [False, False, False]] + >>> m = Matrix([[True, False, False], + ... [False, False, True], + ... [False, False, True]]) + >>> m.is_safe(0, 0, visited) + True + >>> m.is_safe(0, 2, visited) + False + >>> m.is_safe(-1, 2, visited) + False + >>> m.is_safe(1, 5, visited) + False + """ return ( 0 <= i < self.ROW and 0 <= j < self.COL @@ -18,7 +51,21 @@ class Matrix: # Public class to implement a graph ) def diffs(self, i: int, j: int, visited: list[list[bool]]) -> None: - # Checking all 8 elements surrounding nth element + """ + Checking all 8 elements surrounding nth element. + + >>> visited = [[False, False, False], + ... [False, False, False], + ... [False, False, False]] + >>> m = Matrix([[True, True, False], + ... [False, True, False], + ... [True, False, True]]) + >>> m.diffs(0, 0, visited) + >>> visited + [[True, True, False], + [False, True, False], + [True, False, True]] + """ row_nbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order col_nbr = [-1, 0, 1, -1, 1, -1, 0, 1] visited[i][j] = True # Make those cells visited @@ -26,12 +73,26 @@ class Matrix: # Public class to implement a graph if self.is_safe(i + row_nbr[k], j + col_nbr[k], visited): self.diffs(i + row_nbr[k], j + col_nbr[k], visited) - def count_islands(self) -> int: # And finally, count all islands. + def count_islands(self) -> int: + """ + Counts the number of islands in the matrix. + + >>> m = Matrix([[True, True, False, False], + ... [False, True, False, True], + ... [True, False, False, True]]) + >>> m.count_islands() + 2 + >>> m2 = Matrix([[True, True, False], + ... [True, False, False], + ... [False, False, True]]) + >>> m2.count_islands() + 2 + """ visited = [[False for j in range(self.COL)] for i in range(self.ROW)] count = 0 for i in range(self.ROW): for j in range(self.COL): - if visited[i][j] is False and self.graph[i][j] == 1: + if visited[i][j] is False and self.graph[i][j]: self.diffs(i, j, visited) count += 1 return count From bface6dfa1d8be2c3e10fd3b3b143d5336bdb66f Mon Sep 17 00:00:00 2001 From: CheerfulBear22 <.> Date: Tue, 4 Feb 2025 19:28:28 +0000 Subject: [PATCH 3/5] Fixed accidental code change --- matrix/count_islands_in_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/count_islands_in_matrix.py b/matrix/count_islands_in_matrix.py index 393b4c53f..9eb83f4c3 100644 --- a/matrix/count_islands_in_matrix.py +++ b/matrix/count_islands_in_matrix.py @@ -92,7 +92,7 @@ class Matrix: # Public class to implement a graph count = 0 for i in range(self.ROW): for j in range(self.COL): - if visited[i][j] is False and self.graph[i][j]: + if visited[i][j] is False and self.graph[i][j] == 1: self.diffs(i, j, visited) count += 1 return count From c014af947cb1bb30884a890ce5228369f47bbe7a Mon Sep 17 00:00:00 2001 From: CheerfulBear22 <.> Date: Wed, 5 Feb 2025 19:29:15 +0000 Subject: [PATCH 4/5] Deleted unnecessary comment. Addressing to requested changes on #12555 --- matrix/count_islands_in_matrix.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/matrix/count_islands_in_matrix.py b/matrix/count_islands_in_matrix.py index 9eb83f4c3..2f9a180bc 100644 --- a/matrix/count_islands_in_matrix.py +++ b/matrix/count_islands_in_matrix.py @@ -17,8 +17,8 @@ class Matrix: # Public class to implement a graph 4 >>> m.graph [[True, False, False, False], - [True, False, True, False], - [False, False, True, True]] + ...[True, False, True, False], + ...[False, False, True, True]] """ self.graph = graph self.ROW = len(graph) @@ -26,8 +26,6 @@ class Matrix: # Public class to implement a graph def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool: """ - Checks if the cell (i, j) can be visited. - >>> visited = [[False, False, False], ... [False, False, False], ... [False, False, False]] @@ -63,8 +61,8 @@ class Matrix: # Public class to implement a graph >>> m.diffs(0, 0, visited) >>> visited [[True, True, False], - [False, True, False], - [True, False, True]] + ...[False, True, False], + ...[True, False, True]] """ row_nbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order col_nbr = [-1, 0, 1, -1, 1, -1, 0, 1] @@ -92,7 +90,7 @@ class Matrix: # Public class to implement a graph count = 0 for i in range(self.ROW): for j in range(self.COL): - if visited[i][j] is False and self.graph[i][j] == 1: + if not visited[i][j] and self.graph[i][j]: self.diffs(i, j, visited) count += 1 return count From d02fb3b4537a7ec657faf9a0451b38fe4e681f0a Mon Sep 17 00:00:00 2001 From: CheerfulBear22 <.> Date: Fri, 7 Feb 2025 18:25:50 +0000 Subject: [PATCH 5/5] Removing another unnecessary comment. Addressing a requested change on #12555 --- matrix/count_islands_in_matrix.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/matrix/count_islands_in_matrix.py b/matrix/count_islands_in_matrix.py index 2f9a180bc..9e98b4418 100644 --- a/matrix/count_islands_in_matrix.py +++ b/matrix/count_islands_in_matrix.py @@ -73,8 +73,6 @@ class Matrix: # Public class to implement a graph def count_islands(self) -> int: """ - Counts the number of islands in the matrix. - >>> m = Matrix([[True, True, False, False], ... [False, True, False, True], ... [True, False, False, True]])