Answer by martineau for Finding adjacent cells in a grid without exceptions
For doing something as trivial as adding up the values of up to 4 cells, I'd just write the code for each case—it only requires one line per cell location. It may be longer than using a loop construct,...
View ArticleAnswer by Duncan for Finding adjacent cells in a grid without exceptions
I would do it something like this:def adjacentCells(x, y): neighbours = [(x-1, y), (x, y-1), (x+1, y), (x, y+1)] return sum( world[b][a] if 0 <= b < len(world) and 0 <= a < len(world[b])...
View ArticleFinding adjacent cells in a grid without exceptions
There are times when I have an area in a 2D array, and I need to check for adjacent cells. Normally I would do the following:adjacentCells =...
View Article