-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2d_array.py
More file actions
107 lines (77 loc) · 2.14 KB
/
2d_array.py
File metadata and controls
107 lines (77 loc) · 2.14 KB
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
"""
https://www.hackerrank.com/challenges/2d-array
Context
Given a 2D Array, :
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
We define an hourglass in to be a subset of values with indices
falling in this pattern in 's graphical representation:
a b c
d
e f g
There are hourglasses in , and an hourglass sum is the sum of an hourglass' values.
Task
Calculate the hourglass sum for every hourglass in , then print the maximum hourglass sum.
Note: If you have already solved the Java domain's Java 2D Array challenge,
you may wish to skip this challenge.
Input Format
There are lines of input, where each line contains space-separated integers
describing 2D Array ; every value in will be in the inclusive range of to .
Output Format
Print the largest (maximum) hourglass sum found in .
Sample Input
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0
Sample Output
19
"""
def solve(A):
pattern = [
[1, 1, 1],
[0, 1, 0],
[1, 1, 1],
]
pattern_len_row = len(pattern)
pattern_len_col = len(pattern[0])
max_value = -999999
A_len_row= len(A)
A_len_col = len(A[0])
for row in range(A_len_row - pattern_len_row + 1):
for col in range(A_len_col - pattern_len_col + 1):
pattern_sum = 0
for p_row in range(pattern_len_row):
pattern_sum += sum(
[a * b for a, b in zip(
pattern[p_row], A[row + p_row][col:col + pattern_len_col])])
if max_value < pattern_sum:
max_value = pattern_sum
return max_value
def main():
A = []
for _ in range(6):
A.append(list(map(int, input("").strip().split())))
print(solve(A))
if __name__ == '__main__':
main()
import unittest
class TestSolve(unittest.TestCase):
def test_solve(self):
self.assertEqual(
19,
solve([
[1, 1, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[0, 0, 2, 4, 4, 0],
[0, 0, 0, 2, 0, 0],
[0, 0, 1, 2, 4, 0],
])
)