-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Added doctests for matrix/count_islands_in_matrix.py #12555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
214e18a
16bb4b5
8ecee9a
bface6d
c014af9
d02fb3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,15 +43,32 @@ 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: | ||
# Checking all 8 elements surrounding nth element | ||
""" | ||
Comment on lines
-21
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to define the element in the comments when it's already presented? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That should be sorted now |
||
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 | ||
for k in range(8): | ||
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: | ||
Comment on lines
-29
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not Required initially to define the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, removed |
||
""" | ||
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): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the row and col parameters from the Matrix init and instead derive them from the graph's dimensions. This prevents inconsistencies and possible index errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hopefully that should be good now. Let me know if there's anything else that needs fixing