-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflipping_matrix.py
More file actions
49 lines (31 loc) · 968 Bytes
/
flipping_matrix.py
File metadata and controls
49 lines (31 loc) · 968 Bytes
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
#!/bin/python3
# https://www.hackerrank.com/challenges/flipping-the-matrix/problem
import os
def _iter_swap(m, i, j):
for [x, y] in [
(i, j),
(len(m) - i - 1, j),
(i, len(m) - j - 1),
(len(m) - i - 1, len(m) - j - 1),
]:
yield m[x][y]
def _get_max(m, i, j):
return max(_iter_swap(m, i, j))
def _iter_corner(m):
for i in range(len(m) >> 1):
for j in range(len(m) >> 1):
yield i, j
return
def flippingMatrix(matrix):
return sum([_get_max(matrix, i, j) for [i, j] in _iter_corner(matrix)])
if __name__ == "__main__":
fptr = open(os.environ["OUTPUT_PATH"], "w")
q = int(input().strip())
for q_itr in range(q):
n = int(input().strip())
matrix = []
for _ in range(2 * n):
matrix.append(list(map(int, input().rstrip().split())))
result = flippingMatrix(matrix)
fptr.write(str(result) + "\n")
fptr.close()