Skip to content
Open
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
38 changes: 38 additions & 0 deletions matrix/transpose_of_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
finding tanspose of the given matrix
The transpose of a matrix is a new matrix formed by
flipping the original matrix over its main diagonal,
effectively interchanging its rows and columns
suppose A=[[1,2,3],
[4,5,6],
[7,8,9]]
is the given matrix then it's transpose will be
A^t=[[1,4,7],
[2,5,8],
[3,6,9]]
Apporoach:
1. Create a empty(null) matrix of the same size of the given matrix
2.traverse the given matrix by using nested for loop
3.copy the elements in oppposite order of the traversl
i.e. trans[j][i]=matrix[i][j]
"""


def transpose(matrix: list[list[int]]) -> list[list[int]]:
# create a null matrix of same dimension of given matrix
trans = [[0] * len(matrix) for _ in range(len(matrix[0]))]
for i in range(len(matrix)):
# row wise traversal
for j in range(len(matrix[0])):
# column wise traversal
trans[j][i] = matrix[i][j]
# copying the elements in null matrix
return trans


# checking for main function
if __name__ == "__main__":
# matrix(given)
mat = [[1, 2, 3], [4, 5, 6]]
# call the function with mat as parameter
print(transpose(mat))