Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
two liner solution in Clear category for Moore Neighbourhood by nathanweeks
def count_neighbours(grid, row, col):
coords = ((row-1,col-1), (row-1,col), (row-1,col+1), (row, col-1), (row, col+1), (row+1,col-1), (row+1,col), (row+1,col+1))
return sum(grid[coord[0]][coord[1]] for coord in coords if 0 <= coord[0] < len(grid) and 0 <= coord[1] < len(grid[0]))
Dec. 27, 2015