Пример #1
0
    def neighbors(self, i=None, j=None):
        """ Construct and return Neighborhood of point if point is exists
        """
        if not self.position_exists(i, j):
            raise ValueError(
                "Position ({}, {}) does not exist in a {} x {} matrix".format(
                    i, j, self.m, self.n))

        if (i is None) != (j is None):
            # Only one coordinate provided
            raise ValueError("Cannot get neighbors of a column or row")

        if i == j == None and self.position is None:
            # Both None
            raise ValueError(
                "Cannot retrieve neighbors. Either set start or provide coordinates to mark."
            )

        i, j, _ = self.position

        nbhd = Neighborhood()
        # Keep things ordered clockwise for consistency in code
        # Top
        if i > 0:
            nbhd.top = Position(i - 1, j, self.M[i - 1][j])
        # Right
        if j < self.n - 1:
            nbhd.right = Position(i, j + 1, self.M[i][j + 1])
        # Bottom
        if i < self.m - 1:
            nbhd.bottom = Position(i + 1, j, self.M[i + 1][j])
        # Left
        if j > 0:
            nbhd.left = Position(i, j - 1, self.M[i][j - 1])

        return nbhd