class Solution(object): def isValidSudoku(self, board): """ :type board: List[List[str]] :rtype: bool """ #rule 1: Each row must have the numbers 1-9 occuring just once. #rule 2: Each column must have the numbers 1-9 occuring just once. #rule 3: And the numbers 1-9 must occur just once in each of the 9 sub-boxes of the grid. #check board in 9X9 ncol = len(board) nrow = [len(r) for r in board ] if ncol != 9 or min(nrow) !=9 or max(nrow)!=9: return False for i in range(9): row = board[i] col =[ board[j][i] for j in range(9)] subgrid = [ board[j/3+ 3*(i/3)][j%3 + 3*(i%3)] for j in range(9) ] if self.checkDup(row) != True or self.checkDup(col) != True or self.checkDup(subgrid)!=True: return False return True def checkDup(self, l): Counter = {"1":0 , "2":0, "3":0, "4":0, "5":0, "6":0, "7":0, "8":0, "9":0} for i in l: if i != "." : Counter[i]+=1 if Counter[i] > 1: return False return True test = Solution() soduku = [".87654321","2........","3........","4........","5........","6........","7........","8........","9........"] #print test.isValidSudoku(soduku)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question