I need to implement a method transpose() that returns a new matrix that has been transposed. It also has to print out a string of that matrix. It works when using matrix.transpose() but it doesn't work when using matrix.transpose().transpose() because transpose() returns a string. I can't use numpy or add more input to the methods. How would I go about this?
from copy import deepcopy
class Matrix:
def __init__(self, rows):
self.rows = rows[:]
def transpose(self):
copy = deepcopy(self.rows)
transposed = [[copy[j][i] for j in range(len(copy))] for i in range(len(copy[0]))]
matrix = ''
for element in transposed:
for i in element:
matrix += '%2d' % ((i))
matrix += ' '
matrix = matrix[:-1]
matrix += '\n'
return matrix
a = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])