-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathds hourglass.py
106 lines (75 loc) · 2.13 KB
/
ds hourglass.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
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
# 2D Array - DS
# 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
# An hourglass in is 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 . An hourglass sum is the sum of an hourglass' values. Calculate the hourglass sum for every hourglass in , then print the maximum hourglass sum. The array will always be .
# Example
# -9 -9 -9 1 1 1
# 0 -9 0 4 3 2
# -9 -9 -9 1 2 3
# 0 0 8 6 6 0
# 0 0 0 -2 0 0
# 0 0 1 2 4 0
# The hourglass sums are:
# -63, -34, -9, 12,
# -10, 0, 28, 23,
# -27, -11, -2, 10,
# 9, 17, 25, 18
# The highest hourglass sum is from the hourglass beginning at row , column :
# 0 4 3
# 1
# 8 6 6
# Note: If you have already solved the Java domain's Java 2D Array challenge, you may wish to skip this challenge.
# Function Description
# Complete the function hourglassSum in the editor below.
# hourglassSum has the following parameter(s):
# int arr[6][6]: an array of integers
# Returns
# int: the maximum hourglass sum
# Input Format
# Each of the lines of inputs contains space-separated integers .
# Constraints
# Output Format
# Print the largest (maximum) hourglass sum found in .
import math
import os
import random
import re
import sys
#
# Complete the 'hourglassSum' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY arr as parameter.
#
def hourglassSum(arr):
# Write your code here
maxx = -9999999999
def hgsum(arr,i,j):
summ = 0
for a in [i,i+2]:
for b in range(j,j+3):
summ += arr[a][b]
return summ+arr[i+1][j+1]
for i in range(4):
for j in range(4):
add = hgsum(arr,i,j)
if add>maxx:
maxx = add
return maxx
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
arr = []
for _ in range(6):
arr.append(list(map(int, input().rstrip().split())))
result = hourglassSum(arr)
fptr.write(str(result) + '\n')
fptr.close()