Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
@maxi(paths generator) solution in Clear category for Graphical Key by Phil15
from itertools import product
MOVES = {(i,j) for i,j in product((-1,0,1), repeat=2) if i or j} # all 8 moves
maxi = lambda f: lambda *args: max(f(*args), default=0)
@maxi
def g_key(array, length, path = [(0,0)]):
"""Generate values of path from upper-left corner to lower-right corner,
no longer than given length."""
nb_rows, nb_cols = len(array), len(array[0])
if length >= nb_rows * nb_cols: # here because if not... complexity blow me!
yield sum(sum(row) for row in array)
else:
neighbors = lambda i,j: ((i+I,j+J) for I, J in MOVES
if (0<=i+I
Sept. 25, 2018
Comments: