-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueryboard.py3
executable file
·108 lines (87 loc) · 2.61 KB
/
queryboard.py3
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/python3
from sys import argv
try:
FILE = argv[1]
except NameError:
FILE = 'tests/87'
DATA = open(FILE, 'r').read().splitlines()
class Matrix(object):
""" A simple Python matrix class with
basic operations and operator overloading """
def __init__(self, m, n, init=True):
if init:
self.rows = [[0]*n for x in range(m)]
else:
self.rows = []
self.m = m
self.n = n
def __getitem__(self, idx):
return self.rows[idx]
def __setitem__(self, idx, item):
self.rows[idx] = item
def __str__(self):
s='\n'.join([' '.join([str(item) for item in row]) for row in self.rows])
return s + '\n'
def __repr__(self):
s=str(self.rows)
rank = str(self.getRank())
rep="Matrix: \"%s\", rank: \"%s\"" % (s,rank)
return rep
def reset(self):
""" Reset the matrix data """
self.rows = [[] for x in range(self.m)]
def setRow(self, m, x):
""" Set row #m to value x"""
self.rows[m] = [x for nil in range(self.n) ]
def setCol(self, n, x):
""" Set col #n to value x """
for row in self.rows:
row[n] = x
def queryRow(self, m):
""" Returns the sum of row m """
return sum(self.rows[m])
def queryCol(self, n):
""" Returns the sum of col n """
return sum([row[n] for row in self.rows])
def getRank(self):
return (self.m, self.n)
@classmethod
def _makeMatrix(cls, rows):
m = len(rows)
n = len(rows[0])
# Validity check
if any([len(row) != n for row in rows[1:]]):
raise(MatrixError, "inconsistent row length")
mat = Matrix(m,n, init=False)
mat.rows = rows
return mat
@classmethod
def makeZero(cls, m, n):
""" Make a zero-matrix of rank (mxn) """
rows = [[0]*n for x in range(m)]
return cls.fromList(rows)
@classmethod
def fromList(cls, listoflists):
""" Create a matrix by directly passing a list
of lists """
# E.g: Matrix.fromList([[1 2 3], [4,5,6], [7,8,9]])
rows = listoflists[:]
return cls._makeMatrix(rows)
SIZE = 256
matrix = Matrix.makeZero(SIZE, SIZE)
for line in DATA:
if not line:
continue
parts = line.split(' ')
op = parts[0]
q = int(parts[1])
if len(parts) == 3:
v = int(parts[2])
if op == 'SetCol':
matrix.setCol(q,v)
if op == 'SetRow':
matrix.setRow(q,v)
if op == 'QueryCol':
print(matrix.queryCol(q))
if op == 'QueryRow':
print(matrix.queryRow(q))