Generation

generate functionMon, 26 Dec 2022

A matrix matrix is given. matrix = [ [0,1,2,4,8], [6,2,2,1,9], [3,3,3,3,3], [4,6,7,1,2], [5,7,3,4,0] ] Write code (in pure Python, no third-party libraries, no list comprehensions) that will transpose it (that is, make columns out of rows) into matrix_t: matrix_t = [ [0, 6, 3, 4, 5], [1, 2, 3, 6, 7], [2, 2, 3, 7, 3], [4, 1, 3, 1, 4], [8, 9, 3, 2, 0] ]

# First solution def transpose(matrix): new_matrix = [[] for _ in range(len(matrix[0]))] for i in range(len(matrix[0])): for j in range(len(matrix)): new_matrix[i].append(matrix[j][i]) return new_matrix

Questions about programming?Chat with your personal AI assistant