-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Update spiral_print.py #7674
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
Merged
cclauss
merged 15 commits into
TheAlgorithms:master
from
kondekarshubham123:matrix/spiral_matrix
Oct 26, 2022
Merged
Update spiral_print.py #7674
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
670442e
Update spiral_print.py
kondekarshubham123 a45507e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d0b5a8a
Update matrix/spiral_print.py
kondekarshubham123 f9f7a74
Update matrix/spiral_print.py
kondekarshubham123 506bcd0
Update matrix/spiral_print.py
kondekarshubham123 3722730
Update matrix/spiral_print.py
kondekarshubham123 2ec8191
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9cef49d
Update spiral_print.py
kondekarshubham123 d8110e9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 779b808
Update spiral_print.py
kondekarshubham123 050f3a3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 399d9f4
Update spiral_print.py
kondekarshubham123 c3d48e7
Update spiral_print.py
kondekarshubham123 e0513bb
Update spiral_print.py
kondekarshubham123 fb56b95
Update spiral_print.py
kondekarshubham123 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 hidden or 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 | ||||
---|---|---|---|---|---|---|
|
@@ -7,6 +7,9 @@ | |||||
""" | ||||||
|
||||||
|
||||||
from typing import List | ||||||
|
||||||
|
||||||
def check_matrix(matrix: list[list[int]]) -> bool: | ||||||
# must be | ||||||
matrix = [list(row) for row in matrix] | ||||||
|
@@ -75,8 +78,67 @@ def spiral_print_clockwise(a: list[list[int]]) -> None: | |||||
print("Not a valid matrix") | ||||||
return | ||||||
|
||||||
# Other Easy to undersatnd Approach | ||||||
|
||||||
|
||||||
def spiral_traversal(matrix: List[List[int]]) -> List[int]: | ||||||
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.
Suggested change
|
||||||
""" | ||||||
>>> spiral_traversal([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) | ||||||
[1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] | ||||||
|
||||||
Example: | ||||||
matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] | ||||||
|
||||||
Algorithm: | ||||||
Step 1. first pop the 0 index list. (which is [1,2,3,4] and concatenate the | ||||||
output of [step 2]) | ||||||
Step 2. Now perform matrix’s Transpose operation (Change rows to column | ||||||
and vice versa) and reverse the resultant matrix. | ||||||
Step 3. Pass the output of [2nd step], to same recursive function till | ||||||
base case hits. | ||||||
|
||||||
Dry Run: | ||||||
|
||||||
Stage 1. | ||||||
[1, 2, 3, 4] + spiral_traversal( [[8, 12 ] | ||||||
[7, 11] | ||||||
[6, 10] | ||||||
[5, 9]] | ||||||
) | ||||||
|
||||||
Stage 2. | ||||||
[1, 2, 3, 4, 8, 12] + spiral_traversal( [[11, 10, 9], | ||||||
[7, 6, 5]] | ||||||
) | ||||||
|
||||||
Stage 3. | ||||||
[1, 2, 3, 4, 8, 12, 11, 10, 9] + spiral_traversal( [[5], | ||||||
[6], | ||||||
[7] | ||||||
]) | ||||||
|
||||||
Stage 4. | ||||||
[1, 2, 3, 4, 8, 12, 11, 10, 9, 5] + spiral_traversal( [[5], | ||||||
[6], | ||||||
[7] | ||||||
]) | ||||||
|
||||||
Stage 5. | ||||||
[1, 2, 3, 4, 8, 12, 11, 10, 9, 5] + spiral_traversal( [[6, 7] | ||||||
]) | ||||||
|
||||||
Stage 6. | ||||||
[1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] + spiral_traversal([]) | ||||||
kondekarshubham123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
|
||||||
kondekarshubham123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
""" | ||||||
return matrix and list(matrix.pop(0)) + spiral_traversal(list(zip(*matrix))[::-1]) | ||||||
|
||||||
|
||||||
# driver code | ||||||
if __name__ == "__main__": | ||||||
import doctest | ||||||
|
||||||
doctest.testmod() | ||||||
kondekarshubham123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] | ||||||
spiral_print_clockwise(a) |
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.
Uh oh!
There was an error while loading. Please reload this page.