Monday, April 4, 2022

Matrix Transpose Python Codewars

Task:

Write a function that outputs the transpose of a matrix - a new matrix where the columns and rows of the original are swapped.

For example, the transpose of:

| 1 2 3 |
| 4 5 6 |

is

| 1 4 |
| 2 5 |
| 3 6 |

The input to your function will be an array of matrix rows. You can assume that each row has the same length, and that the height and width of the matrix are both positive.

Solution:


def transpose(matrix):
    return [list(x) for x in zip(*matrix)]

print(transpose([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))

Matrix Transpose Solutions | Codewars



Related Posts


No comments :

Post a Comment