From b0e81b5b472cf4f2ff7d4f2c8a6c435e126431bd Mon Sep 17 00:00:00 2001 From: Lusina <12752833+BrianLusina@users.noreply.github.com> Date: Mon, 2 Mar 2026 09:31:22 +0300 Subject: [PATCH 1/2] feat(algorithms, matrix): transpose matrix --- algorithms/matrix/transpose/README.md | 156 ++++++++++++++++++ algorithms/matrix/transpose/__init__.py | 38 +++++ .../matrix/transpose}/test_transpose.py | 4 +- .../matrix/transpose/test_transpose_matrix.py | 47 ++++++ algorithms/transpose/README.md | 67 -------- algorithms/transpose/__init__.py | 11 -- bit_manipulation/sum_two_integers/__init__.py | 4 +- .../test_sum_of_two_integers.py | 3 +- 8 files changed, 246 insertions(+), 84 deletions(-) create mode 100644 algorithms/matrix/transpose/README.md create mode 100644 algorithms/matrix/transpose/__init__.py rename {tests/algorithms => algorithms/matrix/transpose}/test_transpose.py (97%) create mode 100644 algorithms/matrix/transpose/test_transpose_matrix.py delete mode 100644 algorithms/transpose/README.md delete mode 100644 algorithms/transpose/__init__.py diff --git a/algorithms/matrix/transpose/README.md b/algorithms/matrix/transpose/README.md new file mode 100644 index 00000000..03e636c2 --- /dev/null +++ b/algorithms/matrix/transpose/README.md @@ -0,0 +1,156 @@ +# Transpose + +Take input text and output it transposed. + +Given an input text output it transposed. + +Roughly explained, the transpose of a matrix: + +``` +ABC +DEF +``` + +is given by: + +``` +AD +BE +CF +``` + +Rows become columns and columns become rows. See . + +If the input has rows of different lengths, this is to be solved as follows: + +- Pad to the left with spaces. +- Don't pad to the right. + +Therefore, transposing this matrix: + +``` +ABC +DE +``` + +results in: + +``` +AD +BE +C +``` + +And transposing: + +``` +AB +DEF +``` + +results in: + +``` +AD +BE + F +``` + +In general, all characters from the input should also be present in the transposed output. That means that if a column +in the input text contains only spaces on its bottom-most row(s), the corresponding output row should contain the spaces +in its right-most column(s). + +## Source + +Reddit r/dailyprogrammer challenge #270 [Easy] +. [https://www.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text](https://www.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text) + + +-- + +# Transpose Matrix + +You're given a 2D array of integers matrix. Write a function that returns the transpose of the matrix. + +The transpose of a matrix is a flipped version of the original matrix across its main diagonal (which runs from top-left +to bottom-right); it switches the row and column indices of the original matrix. + +You can assume the input matrix always has at least 1 value; however its width and height are not necessarily the same. + +## Examples + +Example 1: + +```text +Input: +matrix = [ + [1, 2], +] + +Output: +[ + [1], + [2] +] +``` + +Example 2: + +```text +Input: +matrix = [ + [1, 2], + [3, 4], + [5, 6], +] + +Output: +[ + [1, 3, 5], + [2, 4, 6], +] +``` + +Example 3: + +```text +Input: +matrix = [ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9], +] + +Output: +[ + [1, 4, 7], + [2, 5, 8], + [3, 6, 9], +] +``` + +## Hints + +- The row and column indices of each entry in the matrix should be flipped. For example, the value at `matrix[1][2]` will + be at `matrix[2][1]` in the transpose of the matrix. +- Each column in the matrix should be become a row in the transpose of the matrix. Each row in the matrix should become + a column in the transpose of the matrix. +- Try iterating one column at a time, and with each column, create a row of the values to add to the transpose of the + matrix. + +## Solution + +The transpose of a matrix `A` with dimensions `R x C` is a matrix `ans` with dimensions C x R for which `ans[c][r]` = `A[r][c]`. + +Let's initialize a new matrix ans representing the answer. Then, we'll copy each entry of the matrix as appropriate. + +### Complexity Analysis + +#### Time Complexity: O(R*C) + +Where `R` and `C` are the number of rows and columns in the given matrix `A`. + +#### Space Complexity: O(R*C) + +The space used by the answer. + diff --git a/algorithms/matrix/transpose/__init__.py b/algorithms/matrix/transpose/__init__.py new file mode 100644 index 00000000..f9dbaa1c --- /dev/null +++ b/algorithms/matrix/transpose/__init__.py @@ -0,0 +1,38 @@ +from typing import List + + +def transpose(input_lines: str) -> str: + """ + transposes letters in input lines like a matrix, where the columns become rows and rows + become columns + :param input_lines: lines to transpose + :return: String with the characters transposed + """ + lines = input_lines.split("\n") + zipped = map(list, [line.ljust(len(max(lines, key=len))) for line in lines]) + + return "\n".join("".join(line) for line in zip(*zipped)).strip() + + +def transpose_matrix(matrix: List[List[int]]) -> List[List[int]]: + """ + transposes a matrix by making the columns the rows and the rows the columns + Args: + matrix: matrix to transpose + Returns: + Transposed matrix + """ + if not matrix: + return [] + + n_rows = len(matrix) + n_cols = len(matrix[0]) + + transposed_matrix = [[0] * n_rows for _ in range(n_cols)] + + for row in range(n_rows): + for col in range(n_cols): + value = matrix[row][col] + transposed_matrix[col][row] = value + + return transposed_matrix diff --git a/tests/algorithms/test_transpose.py b/algorithms/matrix/transpose/test_transpose.py similarity index 97% rename from tests/algorithms/test_transpose.py rename to algorithms/matrix/transpose/test_transpose.py index a3cdd8eb..294648a9 100644 --- a/tests/algorithms/test_transpose.py +++ b/algorithms/matrix/transpose/test_transpose.py @@ -1,8 +1,6 @@ import unittest -from algorithms.transpose import transpose - -# test cases adapted from `x-common//canonical-data.json` @ version: 1.0.0 +from algorithms.matrix.transpose import transpose class TransposeTests(unittest.TestCase): diff --git a/algorithms/matrix/transpose/test_transpose_matrix.py b/algorithms/matrix/transpose/test_transpose_matrix.py new file mode 100644 index 00000000..73f149d9 --- /dev/null +++ b/algorithms/matrix/transpose/test_transpose_matrix.py @@ -0,0 +1,47 @@ +import unittest +from typing import List +from parameterized import parameterized +from algorithms.matrix.transpose import transpose_matrix + +TRANSPOSE_MATRIX_TEST_CASES = [ + ( + [ + [1, 2], + ], + [[1], [2]], + ), + ( + [ + [1, 2], + [3, 4], + [5, 6], + ], + [ + [1, 3, 5], + [2, 4, 6], + ], + ), + ( + [ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9], + ], + [ + [1, 4, 7], + [2, 5, 8], + [3, 6, 9], + ], + ), +] + + +class TransposeMatrixTestCase(unittest.TestCase): + @parameterized.expand(TRANSPOSE_MATRIX_TEST_CASES) + def test_transpose_matrix(self, matrix: List[List[int]], expected: List[List[int]]): + actual = transpose_matrix(matrix) + self.assertEqual(actual, expected) + + +if __name__ == "__main__": + unittest.main() diff --git a/algorithms/transpose/README.md b/algorithms/transpose/README.md deleted file mode 100644 index ca05c1a6..00000000 --- a/algorithms/transpose/README.md +++ /dev/null @@ -1,67 +0,0 @@ -# Transpose - -Take input text and output it transposed. - -Given an input text output it transposed. - -Roughly explained, the transpose of a matrix: - -``` -ABC -DEF -``` - -is given by: - -``` -AD -BE -CF -``` - -Rows become columns and columns become rows. See . - -If the input has rows of different lengths, this is to be solved as follows: - -- Pad to the left with spaces. -- Don't pad to the right. - -Therefore, transposing this matrix: - -``` -ABC -DE -``` - -results in: - -``` -AD -BE -C -``` - -And transposing: - -``` -AB -DEF -``` - -results in: - -``` -AD -BE - F -``` - -In general, all characters from the input should also be present in the transposed output. That means that if a column -in the input text contains only spaces on its bottom-most row(s), the corresponding output row should contain the spaces -in its right-most column(s). - -## Source - -Reddit r/dailyprogrammer challenge #270 [Easy] -. [https://www.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text](https://www.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text) - diff --git a/algorithms/transpose/__init__.py b/algorithms/transpose/__init__.py deleted file mode 100644 index e3877e4e..00000000 --- a/algorithms/transpose/__init__.py +++ /dev/null @@ -1,11 +0,0 @@ -def transpose(input_lines): - """ - transposes letters in input lines like a matrix, where the columns become rows and rows - become columns - :param input_lines: lines to transpose - :return: String with the characters transposed - """ - lines = input_lines.split("\n") - zipped = map(list, [line.ljust(len(max(lines, key=len))) for line in lines]) - - return "\n".join("".join(line) for line in zip(*zipped)).strip() diff --git a/bit_manipulation/sum_two_integers/__init__.py b/bit_manipulation/sum_two_integers/__init__.py index 31685003..630470b5 100644 --- a/bit_manipulation/sum_two_integers/__init__.py +++ b/bit_manipulation/sum_two_integers/__init__.py @@ -27,10 +27,10 @@ def integer_addition(a: int, b: int): def integer_addition_2(a: int, b: int) -> int: mask = 0xFFFFFFFF - max_int = 2 ** 31 - 1 + max_int = 2**31 - 1 while b != 0: sum_ = (a ^ b) & mask carry = (a & b) & mask a = sum_ b = carry << 1 - return a if a <= max_int else ~(a ^ mask) \ No newline at end of file + return a if a <= max_int else ~(a ^ mask) diff --git a/bit_manipulation/sum_two_integers/test_sum_of_two_integers.py b/bit_manipulation/sum_two_integers/test_sum_of_two_integers.py index 29c5a6d9..65fcb9c9 100644 --- a/bit_manipulation/sum_two_integers/test_sum_of_two_integers.py +++ b/bit_manipulation/sum_two_integers/test_sum_of_two_integers.py @@ -13,6 +13,7 @@ (2147483647, 1, -2147483648), ] + class SumOfTwoIntegersTestCase(unittest.TestCase): @parameterized.expand(SUM_OF_TWO_INTEGERS_TEST_CASES) def test_integer_addition(self, a: int, b: int, expected: int): @@ -25,5 +26,5 @@ def test_integer_addition_2(self, a: int, b: int, expected: int): self.assertEqual(result, expected) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() From 53a8aa17509a81a86bcaad25b1170c430915def1 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 2 Mar 2026 06:31:43 +0000 Subject: [PATCH 2/2] updating DIRECTORY.md --- DIRECTORY.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1d48dd6f..648a5ad4 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -268,6 +268,9 @@ * [Test Best Meeting Point](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/matrix/best_meeting_point/test_best_meeting_point.py) * Isvalidsudoku * [Test Is Valid Sudoku](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/matrix/isvalidsudoku/test_is_valid_sudoku.py) + * Transpose + * [Test Transpose](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/matrix/transpose/test_transpose.py) + * [Test Transpose Matrix](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/matrix/transpose/test_transpose_matrix.py) * Memoization * [Fibonacci](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/memoization/fibonacci.py) * [Petethebaker](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/petethebaker.py) @@ -1105,7 +1108,6 @@ * [Test Robot Name](https://github.com/BrianLusina/PythonSnips/blob/master/tests/algorithms/test_robot_name.py) * [Test Say Number](https://github.com/BrianLusina/PythonSnips/blob/master/tests/algorithms/test_say_number.py) * [Test Space Age](https://github.com/BrianLusina/PythonSnips/blob/master/tests/algorithms/test_space_age.py) - * [Test Transpose](https://github.com/BrianLusina/PythonSnips/blob/master/tests/algorithms/test_transpose.py) * [Test Tree Building](https://github.com/BrianLusina/PythonSnips/blob/master/tests/algorithms/test_tree_building.py) * [Test Variable Length Qty](https://github.com/BrianLusina/PythonSnips/blob/master/tests/algorithms/test_variable_length_qty.py) * Cryptography