Clean and abstract Python solution

  • -4

    Here is my Python solution with re and functional programming.

    import re
    def andt(x,y):
        return x and y
    def add(x,y):
        return x+y
    class Solution(object):
        def isValid(self, s):
            for i in range(0,10):
                c = chr(ord('0')+i)
                p = re.compile('(.*'+c+'.*)' +'(.*'+c+'.*)+')
                if p.match(s):
                    return False
            return True
        def isValidSudoku(self, boardt):
            :type board: List[List[str]]
            :rtype: bool
            board = [reduce(add, [x for x in y]) for y in boardt]
            return reduce(andt, map(self.isValid,board), True) and reduce(andt, map(self.isValid,[reduce(add, [x[i] for x in board]) for i in range(0,9)]) ) and reduce(andt,map(self.isValid, [reduce(add, [board[3*i+k][3*j:3*j+3] for k in range(3)]) for i in range(3) for j in range(3)]))

Log in to reply

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