The prompt for the question is linked here:Hourglass sum in 2D array
I have written 2 different codes that are supposed to output the same thing.
Code1
def hourglassSum(arr):
total = []
for i in range(0, 4):
for j in range(0, 4):
total.append(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])
return max(total)
Code2
def hourglassSum(arr):
total = []
for i in range(0, 4):
for j in range(0, 4):
total.append(sum(arr[i][j:j+2]) + arr[i+1][j+1] + sum(arr[i+2][j:j+2]))
return max(total)
The 2nd code outputs a different value. Can anyone tell me what went wrong?
[j:j+3]rather than[j:j+2](i.e. slice is not inclusive of the right end point).