-
-
Notifications
You must be signed in to change notification settings - Fork 46.3k
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
CheerfulBear22
wants to merge
6
commits into
TheAlgorithms:master
Choose a base branch
from
CheerfulBear22:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
214e18a
Added doctests for matrix/count_islands_in_matrix.py
16bb4b5
Changed matrix initialisation in count_islands_in_matrix.py. Addressi…
8ecee9a
Fixing merge conflicts
bface6d
Fixed accidental code change
c014af9
Deleted unnecessary comment. Addressing to requested changes on #12555
d02fb3b
Removing another unnecessary comment. Addressing a requested change o…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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 | ||
""" | ||
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
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([[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): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
That should be sorted now