Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "transpose_matrix"
}
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_nonsymetric_transpose() -> None:
def test_nonsymmetric_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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
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 = [
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]]
assert transpose_matrix(input_1) == expected_1
input_2 = [

["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
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,31 @@ def is_palindrome(sequence: list | str) -> bool:
########################### END ADVENTURE PACK CODE ############################"
`;

exports[`App can equip single goody: Python 3 transpose_matrix 1`] = `
"########################## BEGIN ADVENTURE PACK CODE ###########################
# Adventure Pack commit fake-commit-hash
# Running at: https://example.com/

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)]

########################### END ADVENTURE PACK CODE ############################"
`;

exports[`App can equip single goody: Python 3 traverse_inorder 1`] = `
"########################## BEGIN ADVENTURE PACK CODE ###########################
# Adventure Pack commit fake-commit-hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,25 @@ exports[`App can render goody: Python 3 is_palindrome 1`] = `
return True"
`;

exports[`App can render goody: Python 3 transpose_matrix 1`] = `
"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)]"
`;

exports[`App can render goody: Python 3 traverse_inorder 1`] = `
"from typing import Generator, Optional

Expand Down
Loading