-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-errors.py
64 lines (49 loc) · 1.61 KB
/
3-errors.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
from sys import stdin
class MatrixError(BaseException):
def __init__(self, matrix1, matrix2):
self.matrix1 = matrix1
self.matrix2 = matrix2
class Matrix:
def __init__(self, m):
self.m = []
for i in range(len(m)):
self.m.append([])
for j in range(len(m[i])):
self.m[i].append(m[i][j])
def __add__(self, other):
if len(self.m) != len(other.m) or \
len(self.m[0]) != len(other.m[0]):
raise MatrixError(self, other)
res = []
for i in range(len(self.m)):
res.append([])
for j in range(len(self.m[i])):
res[i].append(self.m[i][j] + other.m[i][j])
return Matrix(res)
def __mul__(self, n):
return Matrix(list(map(
lambda l: list(map(lambda i: i * n, l)),
self.m
)))
__rmul__ = __mul__
def __str__(self):
return "\n".join(map(lambda i: "\t".join(map(str, i)), self.m))
def size(self):
return len(self.m), len(self.m[0])
def transpose(self):
transposed = []
for i in range(len(self.m[0])):
transposed.append([])
for j in range(len(self.m)):
transposed[i].append(self.m[j][i])
self.m = transposed
return Matrix(transposed)
@staticmethod
def transposed(matrix):
transposed = []
for i in range(len(matrix.m[0])):
transposed.append([])
for j in range(len(matrix.m)):
transposed[i].append(matrix.m[j][i])
return Matrix(transposed)
exec(stdin.read())