-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.py
38 lines (33 loc) · 1.11 KB
/
matrix.py
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
33
34
35
36
37
38
# Creates a Matrix class inheriting from the list class.
# Matrices are initiated with m columns and n rows and all cells set to 0.
class Matrix(list):
def __init__(self, m, n):
list.__init__([])
self.m = m
self.n = n
self.columns = self.m
self.rows = self.n
for i in range(m):
self.append([0] * n)
def formatted_print(self, rjust=6):
for m in self:
string_row = ""
for i in m:
string_row += str(i).rjust(rjust) + " "
print(string_row)
def fprint(self, rjust=6):
self.formatted_print(rjust)
def __str__(self, rjust=6):
full_string = ""
for m in self:
string_row = ""
for i in m:
string_row += str(i).rjust(rjust) + " "
full_string += string_row + "\n"
return full_string
if __name__ == "__main__": # Small testing Matrix.
m1 = Matrix(3,4)
m1[0][0] = 12
m1[2][2] = 72
print(str(m1))
print(m1.m, m1.n, m1.columns, m1.rows)