Renamed matrix_transpose.py to mat_trans.py
This commit is contained in:
parent
12ab1b9711
commit
ccacab51ea
32
mat_trans.py
Normal file
32
mat_trans.py
Normal file
@ -0,0 +1,32 @@
|
||||
|
||||
from typing import List
|
||||
"""
|
||||
Transpose a given matrix.
|
||||
Args:
|
||||
M (List[List]): A 2D list representing the matrix to be transposed.
|
||||
Returns:
|
||||
List[List]: The transposed matrix.
|
||||
Example:
|
||||
>>> matrix = [
|
||||
... [1, 2, 3],
|
||||
... [4, 5, 6]
|
||||
... ]
|
||||
>>> matrix_transpose(matrix)
|
||||
[[1, 4], [2, 5], [3, 6]]
|
||||
|
||||
NOTE: If you import the matrix_transpose module into your project, call it with matrix_transpose.transpose(M).
|
||||
|
||||
"""
|
||||
|
||||
def transpose(M) -> List[List]:
|
||||
row = []
|
||||
T = []
|
||||
mcols = len(M[0])
|
||||
nrows = len(M)
|
||||
for i in range(0, mcols):
|
||||
for j in range(0, nrows):
|
||||
row.append(M[j][i])
|
||||
T.append(row)
|
||||
row = []
|
||||
return T
|
||||
|
||||
Loading…
Reference in New Issue
Block a user