Skip to content

Commit e6c6a76

Browse files
Add a transpose Python goody (#371)
Closes #340.
1 parent a9c10d8 commit e6c6a76

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import TypeVar
2+
3+
matrix_element = TypeVar("matrix_element")
4+
5+
6+
def transpose_matrix(
7+
input: list[list[matrix_element]],
8+
) -> list[list[matrix_element]]:
9+
r = len(input)
10+
c = len(input[0]) if r != 0 else 0
11+
if r == 0 or c == 0:
12+
raise ValueError("Can't transpose a matrix that has a 0 dimension")
13+
if any(len(row) != c for row in input):
14+
raise ValueError("Matrix has rows of different lengths")
15+
16+
return [list(row) for row in zip(*input)]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "transpose_matrix"
3+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from . import *
2+
import pytest
3+
4+
5+
def test_improper_input() -> None:
6+
with pytest.raises(ValueError):
7+
transpose_matrix([])
8+
9+
with pytest.raises(ValueError):
10+
transpose_matrix([[], []])
11+
12+
with pytest.raises(ValueError):
13+
transpose_matrix([[0], [0, 1]])
14+
15+
16+
def test_nonsymetric_transpose() -> None:
17+
input_1 = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
18+
expected_1 = [[0, 4, 8], [1, 5, 9], [2, 6, 10], [3, 7, 11]]
19+
input_2 = [
20+
["0", "1", "2"],
21+
["3", "4", "5"],
22+
["6", "7", "8"],
23+
["9", "10", "11"],
24+
]
25+
expected_2 = [
26+
["0", "3", "6", "9"],
27+
["1", "4", "7", "10"],
28+
["2", "5", "8", "11"],
29+
]
30+
input_3 = [[3, 1, 4], [1, 5, 9], [2, 6, 5], [3, 5, 8]]
31+
expected_3 = [[3, 1, 2, 3], [1, 5, 6, 5], [4, 9, 5, 8]]
32+
actual_1 = transpose_matrix(input_1)
33+
actual_2 = transpose_matrix(input_2)
34+
actual_3 = transpose_matrix(input_3)
35+
assert expected_1 == actual_1
36+
assert expected_2 == actual_2
37+
assert expected_3 == actual_3
38+
39+
40+
def test_symetric_transpose() -> None:
41+
input_1 = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
42+
expected = [[0, 3, 6], [1, 4, 7], [2, 5, 8]]
43+
actual = transpose_matrix(input_1)
44+
45+
assert expected == actual

workspaces/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,6 +2830,31 @@ def is_palindrome(sequence: list | str) -> bool:
28302830
########################### END ADVENTURE PACK CODE ############################"
28312831
`;
28322832

2833+
exports[`App can equip single goody: Python 3 transpose_matrix 1`] = `
2834+
"########################## BEGIN ADVENTURE PACK CODE ###########################
2835+
# Adventure Pack commit fake-commit-hash
2836+
# Running at: https://example.com/
2837+
2838+
from typing import TypeVar
2839+
2840+
matrix_element = TypeVar("matrix_element")
2841+
2842+
2843+
def transpose_matrix(
2844+
input: list[list[matrix_element]],
2845+
) -> list[list[matrix_element]]:
2846+
r = len(input)
2847+
c = len(input[0]) if r != 0 else 0
2848+
if r == 0 or c == 0:
2849+
raise ValueError("Can't transpose a matrix that has a 0 dimension")
2850+
if any(len(row) != c for row in input):
2851+
raise ValueError("Matrix has rows of different lengths")
2852+
2853+
return [list(row) for row in zip(*input)]
2854+
2855+
########################### END ADVENTURE PACK CODE ############################"
2856+
`;
2857+
28332858
exports[`App can equip single goody: Python 3 traverse_inorder 1`] = `
28342859
"########################## BEGIN ADVENTURE PACK CODE ###########################
28352860
# Adventure Pack commit fake-commit-hash

workspaces/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,25 @@ exports[`App can render goody: Python 3 is_palindrome 1`] = `
16151615
return True"
16161616
`;
16171617
1618+
exports[`App can render goody: Python 3 transpose_matrix 1`] = `
1619+
"from typing import TypeVar
1620+
1621+
matrix_element = TypeVar("matrix_element")
1622+
1623+
1624+
def transpose_matrix(
1625+
input: list[list[matrix_element]],
1626+
) -> list[list[matrix_element]]:
1627+
r = len(input)
1628+
c = len(input[0]) if r != 0 else 0
1629+
if r == 0 or c == 0:
1630+
raise ValueError("Can't transpose a matrix that has a 0 dimension")
1631+
if any(len(row) != c for row in input):
1632+
raise ValueError("Matrix has rows of different lengths")
1633+
1634+
return [list(row) for row in zip(*input)]"
1635+
`;
1636+
16181637
exports[`App can render goody: Python 3 traverse_inorder 1`] = `
16191638
"from typing import Generator, Optional
16201639

0 commit comments

Comments
 (0)