We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#!/bin/python3importmathimportosimportrandomimportreimportsys## Complete the 'hourglassSum' function below.## The function is expected to return an INTEGER.# The function accepts 2D_INTEGER_ARRAY arr as parameter.#defhourglassSum(arr):arrsum=[]defhourglass(arr,i,j):hg=[]forninrange(3):ifj+n>=len(arr[0]):return[]else:hg.append(arr[i][j+n])print(arr[i][j+n])ifi+1>=len(arr):return[]else:hg.append(arr[i+1][j+1])print(arr[i+1][j+1])ifi+2<len(arr):forninrange(3):hg.append(arr[i+2][j+n])else:return[]returnhgforiinrange(len(arr)):forjinrange(len(arr[0])):hg=hourglass(arr,i,j)ifhg!=[]:arrsum.append(sum(hg))returnmax(arrsum)if__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')arr=[]for_inrange(6):arr.append(list(map(int,input().rstrip().split())))result=hourglassSum(arr)fptr.write(str(result)+'\n')fptr.close()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
2D Array - DS
You are viewing a single comment's thread. Return to all comments →