Search code examples
pythonpython-3.xmultidimensional-arraydistance

Get distance to non-empty item in two-dimensional list column


I am trying to make a game of scrabble. I have a board, defined below.

self.board = [[" ", "A ", "B ", "C ", "D ", "E ", "F ", "G ", "H ", "I ", "J ", "K ", "L ", "M ", "N ", "O "],
    ['01', 'TWS', ' ', ' ', 'DLS', ' ', ' ', ' ', 'TWS', ' ', ' ', ' ', 'DLS', ' ', ' ', 'TWS'],
    ['02', ' ', 'DWS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'DWS', ' '],
    ['03', ' ', ' ', 'DWS', ' ', ' ', ' ', 'DLS', ' ', 'DLS', ' ', ' ', ' ', 'DWS', ' ', ' '],
    ['04', 'DLS', ' ', ' ', 'DWS', ' ', ' ', ' ', 'DLS', ' ', ' ', ' ', 'DWS', ' ', ' ', 'DLS'],
    ['05', ' ', ' ', ' ', ' ', 'DWS', ' ', ' ', ' ', ' ', ' ', 'DWS', ' ', ' ', ' ', ' '],
    ['06', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' '],
    ['07', ' ', ' ', 'DLS', ' ', ' ', ' ', 'DLS', ' ', 'DLS', ' ', ' ', ' ', 'DLS', ' ', ' '],
    ['08', 'TWS', ' ', ' ', 'DLS', ' ', ' ', ' ', 'B', 'O', 'G', ' ', 'DLS', ' ', ' ', 'TWS'],
    ['09', ' ', ' ', 'DLS', ' ', ' ', ' ', 'DLS', ' ', 'DLS', ' ', ' ', ' ', 'DLS', ' ', ' '],
    ['10', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' '],
    ['11', ' ', ' ', ' ', ' ', 'DWS', ' ', ' ', ' ', ' ', ' ', 'DWS', ' ', ' ', ' ', ' '],
    ['12', 'DLS', ' ', ' ', 'DWS', ' ', ' ', ' ', 'DLS', ' ', ' ', ' ', 'DWS', ' ', ' ', 'DLS'],
    ['13', ' ', ' ', 'DWS', ' ', ' ', ' ', 'DLS', ' ', 'DLS', ' ', ' ', ' ', 'DWS', ' ', ' '],
    ['14', ' ', 'DWS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'DWS', ' '],
    ['15', 'TWS', ' ', ' ', 'DLS', ' ', ' ', ' ', 'TWS', ' ', ' ', ' ', 'DLS', ' ', ' ', 'TWS']]        

My goal is to find the distance to the nearest letter from any letter. For example, if I called the function on B, it would return

{"up" : 7, "down" : 7, "left" : 7, "right" : 0}

I have experimented with the built-in next function, but I guess my question is, is there an easy way to get the column of a two-dimensional list?

I also have a list of things that should be considered as empty:

emptyList = "TWS", "DWS", "TLS", "DLS"

Please help. Thank you so much!


Solution

  • You could use next, and extract the column with [row[col_num] for row in board] like this:

    def distances(row_num, col_num):
        letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        if not col_num.isdigit():
            col_num = ord(col_num.upper()) - ord('A') + 1
        col = [row[col_num] for row in board]
        row = board[row_num]
        return {
            'right': next((i for i, c in enumerate(row[col_num+1:]) if c in letters), 15-col_num),
            'left': next((i for i, c in enumerate(row[col_num-1:0:-1]) if c in letters), col_num-1),
            'down': next((i for i, c in enumerate(col[row_num+1:]) if c in letters), 15-row_num),
            'up': next((i for i, c in enumerate(col[row_num-1:0:-1]) if c in letters), row_num-1)
        }
    
    print (distances(8, 'H'))
    

    The arguments to the function should be the row number (8) and column number (8) or the corresponding letter H.

    To check that a square is empty the function checks whether the contents are not a single letter (A-Z).