Neighbor Generator

Just a quick gist that I found very useful


class Solution(object):
    def __init__(self, board, click):
        #"""
        :param board: List[List[str]]
        :param click: List[int]
        :return: List[List[str]]
        #"""
        if not board:
            return []

        self.board = board
        self.rs, self.cs = len(self.board), len(self.board[0])
        self.dirs = [(-1, -1), (-1, 0), (-1, +1),     #  x \ y ->
                     ( 0, -1),          ( 0, +1),     #  |
                     (+1, -1), (+1, 0), (+1, +1)]     #  v
                     
    def get_valid_neighbors(self, x, y):
        neighbor = []

        for dir in self.dirs:
            ri, ci = x + dir[0], y + dir[1]
            if 0 <= ri < self.rs and 0 <= ci < self.cs:
                neighbor.append((ri, ci))

        return neighbor