import math
import os
import random
import re
import sys
First, don't import stuff you don't use. As far as I can tell you don't actually use any of those modules.
if __name__ == "__main__":
Second, you haven't written a module useful to import, so don't do if __name__ == "main":
. Also, the rest of your code would throw an exception at line 21 where you do len(arr) with arr undefined. It's fine to not use such a check if it's understood that you don't intend people to import your module.
I would prefer sys.stdin to input, this is only due to python2/3 changing of what input() does. Going forward in purely python3 it'll probably be fine. I would also just use a list comprehension instead of bothering with map in that case.
arr = [[int(_) for _ in sys.stdin.readline().strip().split()]
for i in range(6)]
As far as what is and isn't pythonic, I would argue that a big part of pythonic is readability/maintainability over things like speed (at least assuming we're not changing the big-O order). To that end, I'd suggest defining the hourglass shape as a list and then using that instead of built in indexing.
hourglass = [[1, 1, 1],
[0, 1, 0],
[1, 1, 1]]
Finally, you can use itertools.product to do a lot of the gruntwork in terms of generating nested for loops and then use max on a map of the result:
print(max(map(hourglass.count, itertools.product(i_range, j_range, [arr]))))
All put together:
import sys
import itertools
arr = [[int(_) for _ in sys.stdin.readline().strip().split()]
for i in range(6)]
class Hourglass(object):
hourglass = [[1, 1, 1],
[0, 1, 0],
[1, 1, 1]]
def __init__(self, hourglass_shape=None):
if hourglass_shape is not None:
self.hourglass = hourglass_shape
def count(self, info):
i, j, array_ = info
return sum([sum([a*b for a,b in zip(array_line[j:], hourglass_line)])
for array_line, hourglass_line in zip(array_[i:],
self.hourglass)])
hourglass = Hourglass()
i_range = range(len(arr) - len(hourglass.hourglass) + 1)
# Assuming rectangular
j_range = range(len(arr[0]) - len(hourglass.hourglass[0]) + 1)
print(max(map(hourglass.count, itertools.product(i_range, j_range, [arr]))))
Most importantly, "pythonic" tends to mean easily readable and understandable by those who understand python well. So:
- Don't import things you don't plan to use. This will make it take a little longer for others to digest your code as their first assumption will be that you're using something from each of those modules.
- Don't do
if __name__ == "main":
unless you plan to write a module for import.
- Don't hard code stuff in (your array indexing).
- Use list comprehension where it makes sense.
- Use builtins like
max
and sum
wherever it makes sense.
Other stuff like how I use itertools.product
and map
'd the result are less "pythonic" vs "non-pythonic" and just how I'd do them to reduce lines of code.
Finally, the question of how "pythonic" your code is really only matters if you plan on sharing it. Python is great for whipping up quick results to (real world versions of) questions like these. If all you're looking for is a quick answer, do whatever works quickest for you. Often the time it would take you to worry about how you're writing code for a problem like this can take longer than it could ever save.