I came up with this code as a solution to a contest question. The original question can be viewed here for attribution sake.: Hackerrank 2d array problem
Sample input is like this;this:
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0
Sample output is like this; 19this:
19
My actual code input from the submission website is as follows;follows:
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())))
total = 0
max_total = -1073741824
for i in range(len(arr)):
for j in range(len(arr[i])):
if (j+2 < 6) and (i+2 < 6):
total = 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]
if max_total < total:
max_total = total
print(max_total)
There were several code listings on a different stack overflowStackOverflow posting C++/Ruby solutions to 2d array problem. Being an absolute beginner in Python, I am still coming up to speed with advanced concepts in Python so my solution presented here is "Un"un-pythonic"Pythonic" and am sure not at all timetimes optimized.How How can iI rewrite this code to be more "Pythonic"? I understand list-comprehensions comprehensions, but how can itthey be applied to Multimulti-dimensional arrays like this case? Also, would re-writingrewriting the code to be pythonicPythonic actually optimize the solution?