Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
generator solution in Clear category for Moore Neighbourhood by Cjkjvfnby
def count_neighbours(grid, row, col):
width = len(grid)
height = len(grid[0])
def get_range(size, pos):
return range(max(0, pos - 1), min(pos + 2, size))
def get_neighbors():
for x in get_range(width, row):
for y in get_range(height, col):
if (x, y) != (row, col):
yield grid[x][y]
return sum(get_neighbors())
Oct. 7, 2014
Comments: