Skip to content

Commit 7d1b5e9

Browse files
committed
feat: refactor and add empty classmethod
1 parent 2dbf7f6 commit 7d1b5e9

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

problem_solving/basic/matrix_multiplication.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@
2626

2727

2828
class Matrix:
29-
# __data: list[list[int | float]]
29+
data: list[list[int | float]]
30+
31+
@classmethod
32+
def zeros(cls, rows: int, cols: int):
33+
if rows < 1 and cols < 1:
34+
raise ValueError("invalid rows or columns provided")
35+
return [[0] * rows] * cols
3036

3137
def __init__(self, data: list[list[int | float]]) -> None:
3238
if data.__len__():
@@ -36,19 +42,25 @@ def __init__(self, data: list[list[int | float]]) -> None:
3642
for col in range(1, data.__len__()):
3743
if data[col].__len__() != _len:
3844
raise ValueError("Invalid matrix")
39-
self.__data = data
45+
self.data = data
4046

4147
@property
4248
def size(self):
43-
if self.__data.__len__() == 0:
49+
if self.data.__len__() == 0:
4450
return (0, 0)
45-
return (self.__data[0].__len__(), self.__data.__len__())
51+
return (self.data[0].__len__(), self.data.__len__())
52+
53+
def get_row(self, index: int):
54+
return [d[index] for d in self.data]
55+
56+
def get_column(self, index: int):
57+
return self.data[index]
4658

4759
def __str__(self) -> str:
48-
if self.__data.__len__():
60+
if self.data.__len__():
4961
return "| |"
5062
return "\n".join(
51-
["| " + "".join([f"{i:^5d}" for i in row]) + " |" for row in self.__data]
63+
["| " + "".join([f"{i:^5d}" for i in row]) + " |" for row in self.data]
5264
)
5365

5466
def __mul__(self, other: "Matrix"):

0 commit comments

Comments
 (0)