-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path2D_array.py
More file actions
61 lines (53 loc) · 1.27 KB
/
2D_array.py
File metadata and controls
61 lines (53 loc) · 1.27 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
"""
Given a 6x6 2D Array, arr:
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 'A' in to be a subset of values with indices
falling in this pattern in 'arr' graphical representation:
a b c
d
e f g
Function Description
It should return an integer, the maximum hourglass sum in the array.
arr: an array of integers
Input Format
Each of the 6 lines of inputs 'arr' contains 6 space-separated integers arr[i][j].
Constraints
-9 <= arr[i][j] <= 9
0 <= i,j <= 5
Output Format
Print the largest (maximum) hourglass sum found in 'arr'.
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 hourglass_sum(arr):
rows = len(arr)
columns = len(arr[0])
max_total = True
for i in range(0, rows-2):
for j in range(0, columns-2):
total = arr[i][j] + arr[i][j+1] + arr[i][j+2] + arr[i+1][j+1] \
+ arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2]
if max_total == True:
max_total = total
max_total = max(max_total, total)
return max_total
arr = [[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],
]
print(hourglass_sum(arr))