mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-12 17:49:50 +00:00
Added doctests for matrix/count_islands_in_matrix.py
This commit is contained in:
parent
6c92c5a539
commit
214e18a13b
@ -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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user