-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
rotate_matrix.py: Add type hints for return values #1023
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
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fc03b28
rotate_matrix.py: Add type hints for return values
cclauss 53af475
Fix typo
cclauss 23aebc0
Run the code thru python/black
cclauss 43dd1d2
Fix 270 comment
cclauss 33de60a
Simplify with get_data() and test the alternatives
cclauss 309240d
) * 3
cclauss 0e79901
Update rotate_matrix.py
cclauss 73ed273
Update rotate_matrix.py
poyea 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 |
---|---|---|
@@ -1,99 +1,76 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
In this problem, we want to rotate the matrix elements by 90, 180, 270 (counterclockwise) | ||
Discussion in stackoverflow: | ||
https://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array | ||
In this problem, we want to rotate the matrix elements by 90, 180, 270 (counterclockwise) | ||
Discussion in stackoverflow: | ||
https://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array | ||
""" | ||
|
||
|
||
def rotate_90(matrix: [[]]): | ||
def rotate_90(matrix: [[]]) -> [[]]: | ||
""" | ||
>>> rotate_90([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) | ||
[[4, 8, 12, 16], [3, 7, 11, 15], [2, 6, 10, 14], [1, 5, 9, 13]] | ||
""" | ||
|
||
transpose(matrix) | ||
reverse_row(matrix) | ||
return matrix | ||
|
||
|
||
def rotate_180(matrix: [[]]): | ||
return reverse_row(transpose(matrix)) | ||
|
||
|
||
def rotate_180(matrix: [[]]) -> [[]]: | ||
""" | ||
>>> rotate_180([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) | ||
[[16, 15, 14, 13], [12, 11, 10, 9], [8, 7, 6, 5], [4, 3, 2, 1]] | ||
""" | ||
|
||
reverse_column(matrix) | ||
reverse_row(matrix) | ||
|
||
""" | ||
OR | ||
|
||
reverse_row(matrix) | ||
reverse_column(matrix) | ||
""" | ||
|
||
return matrix | ||
|
||
|
||
def rotate_270(matrix: [[]]): | ||
return reverse_row(reverse_column(matrix)) | ||
# OR.. reverse_column(reverse_row(matrix)) | ||
|
||
|
||
def rotate_270(matrix: [[]]) -> [[]]: | ||
""" | ||
>>> rotate_270([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) | ||
[[13, 9, 5, 1], [14, 10, 6, 2], [15, 11, 7, 3], [16, 12, 8, 4]] | ||
""" | ||
|
||
transpose(matrix) | ||
reverse_column(matrix) | ||
|
||
""" | ||
OR | ||
|
||
reverse_row(matrix) | ||
transpose(matrix) | ||
""" | ||
|
||
return matrix | ||
|
||
return reverse_column(transpose(matrix)) | ||
# OR.. transpose(reverse_column(matrix)) | ||
|
||
|
||
def transpose(matrix: [[]]): | ||
def transpose(matrix: [[]]) -> [[]]: | ||
matrix[:] = [list(x) for x in zip(*matrix)] | ||
return matrix | ||
def reverse_row(matrix: [[]]): | ||
|
||
|
||
def reverse_row(matrix: [[]]) -> [[]]: | ||
matrix[:] = matrix[::-1] | ||
return matrix | ||
|
||
|
||
def reverse_column(matrix: [[]]): | ||
def reverse_column(matrix: [[]]) -> [[]]: | ||
matrix[:] = [x[::-1] for x in matrix] | ||
return matrix | ||
def print_matrix(matrix: [[]]): | ||
|
||
|
||
def print_matrix(matrix: [[]]) -> [[]]: | ||
for i in matrix: | ||
print(*i) | ||
|
||
|
||
if __name__ == '__main__': | ||
if __name__ == "__main__": | ||
matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] | ||
print("\norigin:\n") | ||
print_matrix(matrix) | ||
rotate_90(matrix) | ||
print("\nrotate 90 counterclockwise:\n") | ||
print_matrix(matrix) | ||
print_matrix(rotate_90(matrix)) | ||
|
||
matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] | ||
print("\norigin:\n") | ||
print_matrix(matrix) | ||
rotate_180(matrix) | ||
print("\nrotate 180:\n") | ||
print_matrix(matrix) | ||
print_matrix(rotate_180(matrix)) | ||
|
||
matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] | ||
print("\norigin:\n") | ||
print_matrix(matrix) | ||
rotate_270(matrix) | ||
print("\nrotate 270 counterclockwise:\n") | ||
print_matrix(matrix) | ||
print_matrix(rotate_270(matrix)) |
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.