-
Notifications
You must be signed in to change notification settings - Fork 13
Add a transpose
Python goody
#371
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
16 changes: 16 additions & 0 deletions
16
workspaces/adventure-pack/goodies/python3/src/transpose_matrix/__init__.py
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from typing import TypeVar | ||
|
||
matrix_element = TypeVar("matrix_element") | ||
|
||
|
||
def transpose_matrix( | ||
input: list[list[matrix_element]], | ||
) -> list[list[matrix_element]]: | ||
r = len(input) | ||
c = len(input[0]) if r != 0 else 0 | ||
if r == 0 or c == 0: | ||
raise ValueError("Can't transpose a matrix that has a 0 dimension") | ||
if any(len(row) != c for row in input): | ||
raise ValueError("Matrix has rows of different lengths") | ||
|
||
return [list(row) for row in zip(*input)] |
3 changes: 3 additions & 0 deletions
3
workspaces/adventure-pack/goodies/python3/src/transpose_matrix/goody.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "transpose_matrix" | ||
} |
45 changes: 45 additions & 0 deletions
45
workspaces/adventure-pack/goodies/python3/src/transpose_matrix/test.py
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 | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,45 @@ | ||||||||||||||||||
from . import * | ||||||||||||||||||
import pytest | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def test_improper_input() -> None: | ||||||||||||||||||
with pytest.raises(ValueError): | ||||||||||||||||||
transpose_matrix([]) | ||||||||||||||||||
|
||||||||||||||||||
with pytest.raises(ValueError): | ||||||||||||||||||
transpose_matrix([[], []]) | ||||||||||||||||||
|
||||||||||||||||||
with pytest.raises(ValueError): | ||||||||||||||||||
transpose_matrix([[0], [0, 1]]) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def test_nonsymetric_transpose() -> None: | ||||||||||||||||||
input_1 = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]] | ||||||||||||||||||
expected_1 = [[0, 4, 8], [1, 5, 9], [2, 6, 10], [3, 7, 11]] | ||||||||||||||||||
input_2 = [ | ||||||||||||||||||
Comment on lines
+17
to
+19
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. Let's go a step further and group the asserts too, so the entirety of that test case is co-located!
Suggested change
|
||||||||||||||||||
["0", "1", "2"], | ||||||||||||||||||
["3", "4", "5"], | ||||||||||||||||||
["6", "7", "8"], | ||||||||||||||||||
["9", "10", "11"], | ||||||||||||||||||
] | ||||||||||||||||||
expected_2 = [ | ||||||||||||||||||
["0", "3", "6", "9"], | ||||||||||||||||||
["1", "4", "7", "10"], | ||||||||||||||||||
["2", "5", "8", "11"], | ||||||||||||||||||
] | ||||||||||||||||||
input_3 = [[3, 1, 4], [1, 5, 9], [2, 6, 5], [3, 5, 8]] | ||||||||||||||||||
expected_3 = [[3, 1, 2, 3], [1, 5, 6, 5], [4, 9, 5, 8]] | ||||||||||||||||||
actual_1 = transpose_matrix(input_1) | ||||||||||||||||||
actual_2 = transpose_matrix(input_2) | ||||||||||||||||||
actual_3 = transpose_matrix(input_3) | ||||||||||||||||||
assert expected_1 == actual_1 | ||||||||||||||||||
assert expected_2 == actual_2 | ||||||||||||||||||
assert expected_3 == actual_3 | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def test_symetric_transpose() -> None: | ||||||||||||||||||
input_1 = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] | ||||||||||||||||||
expected = [[0, 3, 6], [1, 4, 7], [2, 5, 8]] | ||||||||||||||||||
actual = transpose_matrix(input_1) | ||||||||||||||||||
|
||||||||||||||||||
assert expected == actual |
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
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
Oops, something went wrong.
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.