-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16_matrix_class_exercise.py
More file actions
32 lines (27 loc) · 962 Bytes
/
16_matrix_class_exercise.py
File metadata and controls
32 lines (27 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Matrix:
"""A class to represent a square matrix and perform various operations on it."""
def __init__(self, matrix: list[list[int]]) -> None:
self.matrix = matrix
def get_diagonal(self) -> list[int]:
if not self.matrix:
return []
n = len(self.matrix)
return [self.matrix[i][i] for i in range(n)]
def get_counter_diagonal(self) -> list[int]:
if not self.matrix:
return []
n = len(self.matrix)
return [self.matrix[i][n - 1 - i] for i in range(n)]
def rotate_rows(self, k: int) -> None:
if not self.matrix:
return
n = len(self.matrix)
k = k % n
self.matrix[:] = self.matrix[k:] + self.matrix[:k]
def rotate_columns(self, k: int) -> None:
if not self.matrix:
return
n = len(self.matrix[0])
k = k % n
for row in self.matrix:
row[:] = row[k:] + row[:k]