Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Moore Neighbourhood by Tarty
def count_neighbours(grid, row, col):
checkPos = lambda r,c,sizeR, sizeC: ((r >= 0 and r < sizeR) and (c >= 0 and c < sizeC)) or False
radius = 1
neighboors = 0
for r in range(row - radius, row + radius + 1):
for c in range(col - radius, col + radius + 1):
if not(c == col and r == row) and checkPos(r, c, len(grid), len(grid[0])):
neighboors += grid[r][c]
return neighboors
Oct. 16, 2014
Comments: