Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
10-liner solution in Clear category for Hypercube by Merzix
from itertools import product
def hypercube(grid, target='hypercube'):
h, w, prev = range(len(grid)), range(len(grid[0])), None
is_valid = lambda a, b: any(a[i] == b[i] and abs(a[1-i] - b[1-i]) == 1 for i in (0,1))
for letter in target:
points = [(i, j) for i, j in product(h, w) if letter == grid[i][j].lower() and \
(prev is None or any(is_valid((i, j), p) for p in prev))]
if not points: return False
prev = points
return True
if __name__ == '__main__':
assert hypercube([
['g', 'f', 'H', 'Y', 'v'],
['z', 'e', 'a', 'P', 'u'],
['s', 'B', 'T', 'e', 'y'],
['k', 'u', 'c', 'R', 't'],
['l', 'O', 'k', 'p', 'r']]) == True
assert hypercube([
['H', 'a', 't', 's', 'E'],
['a', 'Y', 'p', 'u', 'B'],
['a', 'a', 'P', 'y', 'U'],
['x', 'x', 'x', 'E', 'C'],
['z', 'z', 'z', 'z', 'R']]) == False
Sept. 27, 2018
Comments: