Define a Matrix class to represent a square 2-D grid of int numbers. Include a constructor that takes one required int argument (for the number of rows and columns, which is the same since the matrix is square) and initializes all values to 0. Store the values in a self.data attribute. For example, Matrix(3) would initialize self.data as a list of lists of ints representing a 2-D grid with 3 rows and 3 columns. 2. Define a Matrix __str__(self) -> str method that would produce a string representation of a Matrix object. 3. Write a Matrix method set_value(self, i: int, j: int, val: int) that assigns val to the matrix value in the i-th row and j-th column of self.data. 4. Write a method get_diagonal(self) -> List[int] that returns the list of diagonal elements (from the top-left element to the bottom-right element). For example, if the matrix contains
1. Define a Matrix class to represent a square 2-D grid of int numbers. Include a constructor that takes one required int argument (for the number of rows and columns, which is the same since the matrix is square) and initializes all values to 0. Store the values in a self.data attribute. For example, Matrix(3) would initialize self.data as a list of lists of ints representing a 2-D grid with 3 rows and 3 columns.
2. Define a Matrix __str__(self) -> str method that would produce a string representation of a Matrix object.
3. Write a Matrix method set_value(self, i: int, j: int, val: int) that assigns val to the matrix value in the i-th row and j-th column of self.data.
4. Write a method get_diagonal(self) -> List[int] that returns the list of diagonal elements (from the top-left element to the bottom-right element). For example, if the matrix contains:
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images