10 line python solution


  • 4
    Z
    def updateBoard(self, board, click):
        (row, col), directions = click, ((-1, 0), (1, 0), (0, 1), (0, -1), (-1, 1), (-1, -1), (1, 1), (1, -1))
        if 0 <= row < len(board) and 0 <= col < len(board[0]):
            if board[row][col] == 'M':
                board[row][col] = 'X'
            elif board[row][col] == 'E':
                n = sum([board[row + r][col + c] == 'M' for r, c in directions if 0 <= row + r < len(board) and 0 <= col + c < len(board[0])])
                board[row][col] = str(n or 'B')
                for r, c in directions * (not n): self.updateBoard(board, [row + r, col + c])
        return board
    

    @StefanPochmann Suggested a one line for loop to replace my map function, the line used to be like this :)
    not n and map(self.updateBoard, (board,) * 8, [(row + d[0], col + d[1]) for d in directions])


  • 0

    Could be shorter if you didn't misunderstand the input and output type. Currently your solution is even incorrect.


  • 0
    Z

    @StefanPochmann can you please remind me which part is incorrect?


  • 0

    For output type it should be clear... there is only one. And when you realize that, you'll also realize which part of the input type it is...


  • 0
    Z

    @StefanPochmann Yeah, you are right, just take a look at the input type again, it is indeed nested list, but I remember when I tried to submit my solution the first time, I got such error:
    TypeError: 'str' object does not support item assignment
    Maybe the input type has changed? or maybe my code was wrong somewhere else.

    Thanks a lot, I really hate my solution at first, just because the input type...


  • 0

    @zhongyuan9817 I'm sure it was already List[List[str]] a few hours before the contest, otherwise my solution wouldn't have worked when I tested it. I doubt it was changed, but I wasn't around for a while.

    Are you keeping the helper function intentionally? You could now get rid of it if you wanted to.


  • 0
    Z

    @StefanPochmann Thanks for the information, I would like to get ride of the inner function, it just I was keep getting run time error after I change it. It works fine locally.

    class Solution(object):
        def updateBoard(self, board, click):
            row, col, directions = click[0], click[1], ((-1, 0), (1, 0), (0, 1), (0, -1), (-1, 1), (-1, -1), (1, 1), (1, -1))
            if 0 <= row < len(board) and 0 <= col < len(board[0]):
                if board[row][col] == 'M':
                    board[row][col] = 'X'
                elif board[row][col] == 'E':
                    n = sum([board[row + r][col + c] == 'M' for r, c in directions if 0 <= row + r < len(board) and 0 <= col + c < len(board[0])])
                    board[row][col] = n and str(n) or 'B'
                    not n and map(self.updateBoard, (board,) * 8, [(row + d[0], col + d[1]) for d in directions])
            return board
    

    It works fine if I call the recursion without using map

    if not n:
        for d in directions:
            self.updateBoard(board, (row + d[0], col + d[1]))
    

    I think the way I used to copy the reference of board object was wrong, as you can see I shadow copied 8 board for each recursive call. What do you think?


  • 1

    @zhongyuan9817 Yeah, I also just found out that your abuse of map prevents you from getting it accepted. Not sure why exactly. But it's appropriate to get punished for that :-P

    A one-liner for-loop that also gets it accepted:

    for r, c in directions * (not n): self.updateBoard(board, [row + r, col + c])
    

    And a shorter version of your first line:

    (row, col), directions = click, ((-1, 0), (1, 0), (0, 1), (0, -1), (-1, 1), (-1, -1), (1, 1), (1, -1))

  • 0
    Z

    I like the one liner loop, I will use it in my code, hope you don't mind. :)


  • 1

    @zhongyuan9817 I don't mind :-)

    Another shorter piece:

    board[row][col] = str(n or 'B')

  • 0
    Z

    @StefanPochmann Cool, I like it, always learn something new from you :-)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.