2021/11.py
def grid_adjs(
    coord: Tuple[int, ...],
    bounds: Tuple[Tuple[int, int], ...] = None,
    adjs_type: AdjacenciesType = AdjacenciesType.COMPASS,
    bounds_type: BoundsType = BoundsType.RANGE,
) -> Iterable[Tuple[int, ...]]:
    """
    Compute the compass adjacencies for a given :math:`n`-dimensional point. Bounds can
    be specified, and only adjacent coordinates within those bounds will be returned.
    Bounds can be specified as any one of the :class:`BoundsType`s.

    :param coord: coordinate to calculate the adjacencies of
    :param bounds: ``(high, low)`` tuples for each of the dimensions
    :param adjs_type: the :class:`AdjacenciesType` to use
    :param bounds_type: the :class:`BoundsType` to use
    """
    # Iterate through all of the deltas for the N dimensions of the coord. A delta is
    # -1, 0, or 1 indicating that the adjacent cell is one lower, same level, or higher
    # than the given coordinate.
    for delta in it.product((-1, 0, 1), repeat=len(coord)):
        if all(d == 0 for d in delta):
            # This is the coord itself, skip.
            continue

        if adjs_type == AdjacenciesType.COMPASS:
            if sum(map(abs, delta)) > 1:
                # For compass adjacencies, we only care when there's only one dimension
                # different than the coordinate.
                continue

        if bounds is not None:
            in_bounds = True
            for i, (d, (low, high)) in enumerate(zip(delta, bounds)):
                if bounds_type == BoundsType.RANGE:
                    in_bounds &= low <= coord[i] + d < high
                elif bounds_type == BoundsType.INCLUSIVE:
                    in_bounds &= low <= coord[i] + d <= high
                elif bounds_type == BoundsType.EXCLUSIVE:
                    in_bounds &= low < coord[i] + d < high
                if not in_bounds:
                    continue

            if not in_bounds:
                continue
        yield tuple(c + d for c, d in zip(coord, delta))

    Compute the compass adjacencies for a given :math:`n`-dimensional point. Bounds can
    be specified, and only adjacent coordinates within those bounds will be returned.
    Bounds can be specified as any one of the :class:`BoundsType`s.

    :param coord: coordinate to calculate the adjacencies of
    :param bounds: ``(high, low)`` tuples for each of the dimensions
    :param adjs_type: the :class:`AdjacenciesType` to use
    :param bounds_type: the :class:`BoundsType` to use
    
Compute the compass adjacencies for a given :math:`n`-dimensional point. Bounds can
be specified, and only adjacent coordinates within those bounds will be returned.
Bounds can be specified as any one of the :class:`BoundsType`s.
Compute the compass adjacencies for a given : math : ` n ` - dimensional point . Bounds can be specified and only adjacent coordinates within those bounds will be returned . Bounds can be specified as any one of the : class : ` BoundsType ` s .
def grid_adjs(
    coord: Tuple[int, ...],
    bounds: Tuple[Tuple[int, int], ...] = None,
    adjs_type: AdjacenciesType = AdjacenciesType.COMPASS,
    bounds_type: BoundsType = BoundsType.RANGE,
) -> Iterable[Tuple[int, ...]]:
    for delta in it.product((-1, 0, 1), repeat=len(coord)):
        if all(d == 0 for d in delta):
            continue
        if adjs_type == AdjacenciesType.COMPASS:
            if sum(map(abs, delta)) > 1:
                continue
        if bounds is not None:
            in_bounds = True
            for i, (d, (low, high)) in enumerate(zip(delta, bounds)):
                if bounds_type == BoundsType.RANGE:
                    in_bounds &= low <= coord[i] + d < high
                elif bounds_type == BoundsType.INCLUSIVE:
                    in_bounds &= low <= coord[i] + d <= high
                elif bounds_type == BoundsType.EXCLUSIVE:
                    in_bounds &= low < coord[i] + d < high
                if not in_bounds:
                    continue
            if not in_bounds:
                continue
        yield tuple(c + d for c, d in zip(coord, delta))
def grid_adjs ( coord : Tuple [ int , ... ] , bounds : Tuple [ Tuple [ int , int ] , ... ] = None , adjs_type : AdjacenciesType = AdjacenciesType . COMPASS , bounds_type : BoundsType = BoundsType . RANGE , ) -> Iterable [ Tuple [ int , ... ] ] : for delta in it . product ( ( - 1 , 0 , 1 ) , repeat = len ( coord ) ) : if all ( d == 0 for d in delta ) : continue if adjs_type == AdjacenciesType . COMPASS : if sum ( map ( abs , delta ) ) > 1 : continue if bounds is not None : in_bounds = True for i , ( d , ( low , high ) ) in enumerate ( zip ( delta , bounds ) ) : if bounds_type == BoundsType . RANGE : in_bounds &= low <= coord [ i ] + d < high elif bounds_type == BoundsType . INCLUSIVE : in_bounds &= low <= coord [ i ] + d <= high elif bounds_type == BoundsType . EXCLUSIVE : in_bounds &= low < coord [ i ] + d < high if not in_bounds : continue if not in_bounds : continue yield tuple ( c + d for c , d in zip ( coord , delta ) )
Compute the compass adjacencies for a given :math:`n`-dimensional point.
Compute the compass adjacencies for a given : math : ` n ` - dimensional point .
"""
    Compute the compass adjacencies for a given :math:`n`-dimensional point. Bounds can
    be specified, and only adjacent coordinates within those bounds will be returned.
    Bounds can be specified as any one of the :class:`BoundsType`s.

    :param coord: coordinate to calculate the adjacencies of
    :param bounds: ``(high, low)`` tuples for each of the dimensions
    :param adjs_type: the :class:`AdjacenciesType` to use
    :param bounds_type: the :class:`BoundsType` to use
    """ # Iterate through all of the deltas for the N dimensions of the coord. A delta is # -1, 0, or 1 indicating that the adjacent cell is one lower, same level, or higher # than the given coordinate. # This is the coord itself, skip. # For compass adjacencies, we only care when there's only one dimension # different than the coordinate.
coord
Tuple[int, ...]
coordinate to calculate the adjacencies of
coordinate to calculate the adjacencies of
bounds
Tuple[Tuple[int, int], ...]
``(high, low)`` tuples for each of the dimensions
` ` ( high low ) ` ` tuples for each of the dimensions
adjs_type
AdjacenciesType
the :class:`AdjacenciesType` to use
the : class : ` AdjacenciesType ` to use
bounds_type
BoundsType
the :class:`BoundsType` to use
the : class : ` BoundsType ` to use