Hackerrank Day 11: 2D Arrays

Context
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

We define an hourglass in  to be 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 , and an hourglass sum is the sum of an hourglass' values.

CODE

#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    arr = []

    for _ in range(6):
        arr.append(list(map(int, input().rstrip().split())))
    maxsum=-9*7
    for i in range(1,5):
        for j in range(1,5):
            result=arr[i][j]+sum(arr[i-1][j-1:j+2])+sum(arr[i+1][j-1:j+2])#注意是冒号
            
            if result >=maxsum:
                maxsum=result
    print(maxsum)
发布了128 篇原创文章 · 获赞 90 · 访问量 4872

猜你喜欢

转载自blog.csdn.net/weixin_45405128/article/details/103927173
今日推荐