# 12572 - Python2020 Quiz7 Problem2 ###### tags: `Quiz` [TOC] ## Description Define a Matrix class to represent numbers as a two-dimensional array. The constructor for the matrix is a list of lists of numbers. A 3x3 matrix ![](https://i.imgur.com/WfD029D.jpg) would be constructed as M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) The Matrix class would support several methods, and the usage is as follows: ```shell >>> import random >>> random.seed(0) # set the seed so that the result of randomize() will be predictable. >>> M = Matrix([[1, 2, 3], [4, 5, 6]]) >>> M = M.randomize() >>> M.row(1) [1, 6, 4] >>> N = Matrix([[1, 2, 3], [4, 5, 6]]) >>> N = N.transpose() >>> N.column(0) [1, 2, 3] ``` #### Input 6 statements. Instantiate a Matrix M. Execute M = M.randomize() Print a specific row of Matrix M. Instantiate a Matrix N. Execute N = N.transpose() Print a specific column of Matrix N. ###### Sample Input ``` M = Matrix([[1, 2, 3], [4, 5, 6]]) M = M.randomize() print(M.row(1)) N = Matrix([[1, 2, 3], [4, 5, 6]]) N = N.transpose() print(N.column(0)) ``` #### Output 2 lines. The specific row of Matrix M after executing M = M.randomize() The specific column of Matrix N after executing N = N.transpose() ###### Sample Output ``` [1, 6, 4] [1, 2, 3] ``` ## Solution ```python import random random.seed(0) # You must initiate the random seed to 0 for judging purpose. class Matrix: def __init__(self, data): # data is the list of lists of value self._data = data def row(self, r): # return the r-th row in the form of a list # r is from 0.. number of rows # in the exmaple above, M.row(1) would return # [4, 5, 6] return self._data[r] def column(self, c): # return a the c-th column in the form of a list. # in the example above, M.column(2) would return # [3, 6, 9] return [i[c] for i in self._data] @property def nrows(self): # return the number of rows return len(self._data) @property def ncolumns(self): # return the number of columns return len(self._data[0]) def transpose(self): # return a new Matrix whose content is same as this # Matrix except the row and column positions are # switched. In the example above, # M.transpose() would return # Matrix([[1, 4, 7], [2, 5, 8], [3, 6, 9]]) # Note: use zip() to do the transpose return Matrix(zip(*self._data)) def randomize(self): # return another matrix whose content is the same as # this matrix except their positions are randomized. # Notice that you must reduce the dimensions of the matrix to one dimension(flatten it) # and then randomize it using random.shuffle, and finally form the two dimensional matrix again. temp = [] for i in self._data: temp.extend(i) random.shuffle(temp) ans = [] for i in range(0, self.nrows*self.ncolumns, self.ncolumns): ans.append(temp[i:i+self.ncolumns]) return Matrix(ans) # For judging purpose instantiate_M = input() exec(instantiate_M) randomize_M = input() exec(randomize_M) print_row_M = input() exec(print_row_M) instantiate_N = input() exec(instantiate_N ) transpose_N = input() exec(transpose_N ) print_row_N = input() exec(print_row_N) ```