Implement the Matrix class. It should contain:

Constructor from the list of lists. It is guaranteed that the lists are made up of numbers, are not empty and all have the same size. The designer must copy the contents of the list of lists, that is, when the lists from which the matrix was constructed are changed, the contents of the matrix should not change. The str method converts a matrix to a row. At the same time, elements within a single line must be separated by tabs, and lines by line breaks. After each line there should not be a tab character and at the end there should not be a line break. The size method with no arguments, which returns a tuple of view (number of rows, number of columns).

By martyrdom, she gathered a "hodgepodge" of similar codes on the forums, it turned out like this:

from sys import stdin from copy import deepcopy class Matrix(object): def __init__(self, matrix): self.matrix = deepcopy(matrix) def __str__(self): return '\n'.join([''.join(['%d\t' % i for i in row]) for row in self.matrix]) @property def size(self): rows = len(self.matrix) cols = 0 for row in self.matrix: if len(row) > cols: cols = len(row) return (rows, cols) # exec(stdin.read()) m = Matrix([[1, 1, 1], [0, 100, 10]]) print(str(m) == '1\t1\t1\n0\t100\t10') 

and here the data that is specified at the very bottom of the code is not working out correctly - it is necessary that True comes out, and False comes out. What is not taken into account?

tests of the testing system with the results:

 Тест 1 Входные данные: # Task 1 check 1 m = Matrix([[1, 0], [0, 1]]) print(m) m = Matrix([[2, 0, 0], [0, 1, 10000]]) print(m) m = Matrix([[-10, 20, 50, 2443], [-5235, 12, 4324, 4234]]) print(m) Вывод программы: 1 0 0 1 2 0 0 0 1 10000 -10 20 50 2443 -5235 12 4324 4234 Тест 2 Входные данные: # Task 1 check 2 m1 = Matrix([[1, 0, 0], [1, 1, 1], [0, 0, 0]]) m2 = Matrix([[1, 0, 0], [1, 1, 1], [0, 0, 0]]) print(str(m1) == str(m2)) Вывод программы: True Тест 3 Входные данные: # Task 1 check 3 m = Matrix([[1, 1, 1], [0, 100, 10]]) print(str(m) == '1\t1\t1\n0\t100\t10') Вывод программы: True 

test 1 on the screen for some reason looks different

    1 answer 1

    In my opinion, you have an extra tab at the end of each line and the returning dimension function works with errors.

    Try this:

     class Matrix(object): def __init__(self, matrix): self.matrix = deepcopy(matrix) def __str__(self): return '\n'.join('\t'.join(map(str,row)) for row in self.matrix) def size(self): return (len(self.matrix), len(self.matrix[0])) 
    • Something now the first tests do not pass because of spaces / tabs ... I will try to sort out on weekends. Anyway, thank you very much for the help! - Lazarevna
    • @Lazarevna, this course does not have free 7-day access like other courses :( - MaxU
    • .... strange .... I pass for free - Lazarevna
    • @Lazarevna, strange, here's what they write: “This course doesn’t have a 7-day full access free trial,“ - MaxU