-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.py
73 lines (61 loc) · 1.86 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Author: Jacob Tsekrekos
# Date: Jun 1, 2018
# File: matrix.py
# Description: Matrix class to store 2-d arrays
class Matrix:
"""
Stored row - displayed row major
starts at upper left hand corner
"""
def __init__(self, x, y, fill=None):
if fill is None:
fill = " "
# self.elements = [["{:>04}".format(i*x + j) for i in range(y)] for j in range(x)]
self.elements = [[fill for i in range(y)] for j in range(x)]
self.__rows = x
self.__columns = y
def __str__(self):
e = [[str(j) for j in i] for i in zip(*self.elements)]
return "\n".join(" ".join(i) for i in e)
def __getitem__(self, item):
return self.elements[item]
def writeMatrix(self, other, x=0, y=0):
"""
Writes another matrix on-top of the current matrix
:param x: x offset
:param y: y offset
:type other: Matrix
:returns: None
"""
# Use self.rows as the range instead?
for i in range(other.rows):
if 0 > i + x or i + x >= self.rows:
continue
for j in range(other.columns):
if 0 > j + y or j + y >= self.columns:
continue
self[i + x][j + y] = other[i][j]
@property
def rows(self):
return self.__rows
@property
def columns(self):
return self.__columns
# class PopMatrix(Matrix):
# __invalid = []
#
# @property
# def indices(self):
# return [[x, y] for x in range(self.rows - 1) for y in range(self.columns - 1) if [x, y] not in self.__invalid]
#
# def remove(self, index):
# self.__invalid.append(index)
#
# def clear(self):
# self.__invalid = []
if __name__ == "__main__":
m = Matrix(10, 5, "+")
print(m, "\n")
o = Matrix(5, 5, "0")
m.writeMatrix(o, 6, -1)
print(m)