Skip to content
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

Added doctests for matrix/count_islands_in_matrix.py #12555

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 66 additions & 5 deletions matrix/count_islands_in_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,15 +51,43 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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([[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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not Required initially to define the function

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, removed

"""
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):
Expand Down