From fa4c0ad98ab3bac6180a3364a8e4012e4bd34ed4 Mon Sep 17 00:00:00 2001 From: Donald Calloway Date: Tue, 30 Sep 2025 17:14:32 -0700 Subject: [PATCH] Adding new file to repo --- square_matrix_transpose.py | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 square_matrix_transpose.py diff --git a/square_matrix_transpose.py b/square_matrix_transpose.py new file mode 100644 index 0000000..6e8fda6 --- /dev/null +++ b/square_matrix_transpose.py @@ -0,0 +1,48 @@ + + +from typing import List +""" +Transposes a square matrix. +Args: + M (List[List]): A square matrix represented as a list of lists. +Returns: + List[List]: The transpose of the input square matrix. +Example: + >>> square_matrix_transpose([ + ... [1, 2, 3, 0], + ... [4, 5, 6, 1], + ... [7, 8, 9, 2], + ... [1, 2, 3, 4] + ... ]) + [[1, 4, 7, 1], [2, 5, 8, 2], [3, 6, 9, 3], [0, 1, 2, 4]] +""" + +def square_matrix_transpose(M) -> List[List]: + row = [] + T = [] + ncols = len(M[0]) # ncols is number of columns in matrix A + for i in range(0, ncols): + for j in range(0, ncols): + row.append(M[j][i]) + T.append(row) + row = [] + return T + +# Example Usage +matrix = [ + [1,2,3,0], + [4,5,6,1], + [7,8,9,2], + [1,2,3,4] +] + +print(square_matrix_transpose(matrix)) + +# Output (As a matrix) +''' + [[1, 4, 7, 1], + [2, 5, 8, 2], + [3, 6, 9, 3], + [0, 1, 2, 4]] + +''' \ No newline at end of file