I would like to learn how to use list comprehension to find the things like the maximum value within a matrix.
This is a simplified version of what I'd like to do.
max_hourglass = 0
[[max_hourglass = i+j for j in range(4) if j < 3] for i in range(4)]
print(max)
I get a syntax error for the assignment of max. I tried casting i
and j
to integers just in case this was the problem, however, it doesn't appear to be the case.
The more complicated problem I'm attempting to use this code for is to solve a HackerRank question. I have already solved it, however, I am just attempting to expand my knowledge of python3
by using different techniques to solve the problem. This is the Hackerrank problem.
Here is the more complicated problems code just in case the problem is different for it.
arr = []
for _ in range(6):
arr.append([int(num) for num in input().split()])
max_hourglass = 0
for i in range(4):
for j in range(4):
hourglass = 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 hourglass > max_hourglass:
max_hourglass = hourglass
print(max_hourglass)
As you can see the problem I am trying to reduce to list comprehension is the nested for loops for which I came up with this code with the same logic as the simplified version:
[max_hourglass = (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]) for j in range(4) if (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]) > max_hourglass] for i in range(4)]
I'm completely aware this is less readable code and I would not use this in production but rather just want to further understand python's list comprehension syntax.
For context, the Hackerrank problem is to find in a 2D array (Matrix) the largest sum of values made up of the shape I
of which they denote as an hourglass shape.
e.g.
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
Would result in the output 19
as the values:
2 4 4
2
1 2 4
add up to 19.