How does Python determine whether a Sudoku is valid?
Introduction
This Sudoku may only contain some numbers, and the missing numbers are represented.
Notes
A legal Sudoku (partial filling only) is not always solvable. We only need to make the filled space valid.
Disintegration ideas
Pre-process the sudoku by row, column, and block, and then determine whether it is legal.
Using Python expression derivation, anonymous functions andall
Functions can be easily processed.
Code
Class Solution: # @ param board, a 9x9 2D array # @ return a booleandef isValidSudoku (self, board): rows = [list (lst [:]) for lst in board] columns = [[lst [idx] for lst in board] for idx in range (9)] blocks_origin = [board [row] [column] for x in [[0, 1, 2], [3, 4, 5], [6, 7, 8] for y in [[0, 1, 2], [3, 4, 5], [6, 7, 8] for row in x for column in y] # use some techniques to directly store data blocks = [[blocks_origin [row * 9 + c Olumn] for column in range (9)] for row in range (9)] check = lambda lst: all ([lst. count (x) = 1 for x in lst if x! = '. ']) # Determine whether a record (nine numbers arranged in a certain way) is legal return all ([check (x) for style in (rows, columns, blocks) for x in style]) # Step by Step
Summary
If you do not need a circular body, try not to use a circular body. The above is all the content of this article. I hope it will be helpful for your study and work. If you have any questions, please leave a message.