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] ]

def transpose(matrix): ''' Transpose a matrix ''' matrix_t = [] for i in range(len(matrix[0])): row = [] for j in range(len(matrix)): row.append(matrix[j][i]) matrix_t.append(row) return matrix_t

Questions about programming?Chat with your personal AI assistant