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) 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(A): rows = len(A) cols = len(A[0]) B = [[0 for i in range(0, rows)] for j in range(0, cols)] for i in range(0, rows): for j in range(0, cols): B[j][i] = A[i][j] return B

Questions about programming?Chat with your personal AI assistant