-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.py
33 lines (24 loc) · 1.03 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
# These functions are written by jadenPete and I, I just
# stole them to make my work a bit easier in this repo
class Matrix(list):
def __init__(self, width, height, fill=" "):
self.extend([fill] * width for _ in range(height))
def __str__(self):
return "\n".join(map("".join, self))
class Table(list):
def __init__(self, iterable=[], just="left", sep=" "):
self.extend(iterable)
self.just = just
self.sep = sep
def str_len(self, string):
return len(str(string))
def __str__(self):
# Find the maximum of each column by rotating the two-dimensional array
widths = [max(map(self.str_len, row)) for row in zip(*self)]
result = []
for row in self:
if self.just == "left":
result.append(self.sep.join(str(value).ljust(widths[i]) for i, value in enumerate(row)))
else:
result.append(self.sep.join(str(value).rjust(widths[i]) for i, value in enumerate(row)))
return "\n".join(result)