Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Moore Neighbourhood with Itertools solution in Clear category for Moore Neighbourhood by arma
from itertools import product
def count_neighbours(grid, row, col):
"""Summ values around the cell with (row, col) coordinates in grid"""
summ = 0
for x, y in product([row - 1, row, row + 1], [col - 1, col, col + 1]):
if x < 0 or y < 0:
continue
if x == row and y == col:
continue
try:
summ += grid[x][y]
except IndexError:
pass
return summ
March 19, 2015
Comments: