diff --git a/1.13/transpose.py b/1.13/transpose.py new file mode 100644 index 0000000..9ae0163 --- /dev/null +++ b/1.13/transpose.py @@ -0,0 +1,20 @@ +""" +Exercise 1.13 - Transpose Matrix +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def transpose(matrix): + """ + Transpose a matrix (nested list). + :param matrix: Input matrix + :return: Transposed matrix + """ + return [[matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))] + + +if __name__ == '__main__': + print(f'{transpose([[1, 2, 3], [4, 5, 6]]) = }') + print(f'{transpose([[1, 2]]) = }') + print(f'{transpose([[3]]) = }')