Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Commented solution with label and generic_filter from scipy.ndimage solution in 3rd party category for Boundary Blocks by rodka81
from scipy.ndimage import label, generic_filter
import numpy as np
def boundary_blocks(grid):
# use scipy.ndimage.label to identify the areas of empty cells
# which are marked as '1' in the 2d np.array below. Blocks are marked as '0'
areas, _ = label(
np.array([list(map(lambda c: int(c == "."), list(row))) for row in grid])
)
# use generic_filter with the footprint below
# to check if a cell has multiple distinct neighboring areas
footprint = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]])
multiple_neighbors = generic_filter(
areas, lambda v: len(set(v) - {0}) > 1, footprint=footprint
)
# return all blocks, which are marked as zeros (thus np.where(areas == 0)),
# if they have multiple neighboring areas
return {tuple(map(int, (x, y))) for x, y in zip(*np.where(areas == 0))
if multiple_neighbors[x, y]}
May 29, 2019
Comments: