Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Respect the grid boundaries & Play With DIRS navigation solution in Clear category for Moore Neighbourhood by Stensen
from operator import add
DIRS = {"S": ( 1, 0), 'SW': (1, -1), 'SE': ( 1, 1 ), "E": (0, 1),
"N": (-1, 0), 'NE': (-1, 1), 'NW': (-1, -1), "W": (0, -1)}
def count_neighbours(grid, row, col, count = 0):
for move in DIRS:
_r, _c = map(add, (row, col), DIRS[move])
try:
if 0 <= _c < len(grid[0]) \
and 0 <= _r < len(grid) and \
grid[_r][_c] == 1: count += 1
except IndexError: continue
return count
Nov. 9, 2020
Comments: