I am trying to solve the hourglass problem on hackerrank.you can find the details of problem here (https://www.hackerrank.com/challenges/2d-array).
On my machine code works fine and give correct results even for the testcase that gives error on hackerrank.
Here is the code:
maxSum = -70
#hourglass = []
arr = [[int(input()) for x in range(0,6)] for y in range(0,6)]
for row in range(0,6):
for col in range(0,6):
if (row + 2) < 6 and (col + 2) < 6 :
sum = arr[row][col] + arr[row][col+1] + arr[row][col+2] + arr[row+1][col+1] + arr[row+2][col] + arr[row+2][col+1] + arr[row+2][col+2]
if sum > maxSum:
#hourglass.append(arr[row][col])
#hourglass.append(arr[row][col+1])
#hourglass.append(arr[row][col+2])
#hourglass.append(arr[row+1][col+1])
#hourglass.append(arr[row+2][col])
#hourglass.append(arr[row+2][col+1])
#hourglass.append(arr[row+2][col+2])
maxSum = sum
print(maxSum)
#print(hourglass)
Following error rased while running code:
Traceback (most recent call last):
File "solution.py", line 4, in <module>
arr = [[int(input()) for x in range(0,6)] for y in range(0,6)]
File "solution.py", line 4, in <listcomp>
arr = [[int(input()) for x in range(0,6)] for y in range(0,6)]
File "solution.py", line 4, in <listcomp>
arr = [[int(input()) for x in range(0,6)] for y in range(0,6)]
ValueError: invalid literal for int() with base 10: '1 1 1 0 0 0'
The testcase for which error is raised is:
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 9 2 -4 -4 0
0 0 0 -2 0 0
0 0 -1 -2 -4 0