Created
November 5, 2020 17:00
-
-
Save ahmed4end/894b1164b20f824148b21bbe9786548e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python - How to check for a valid sudoku. | |
sudoku = [[8, 7, 5, 6, 2, 1, 9, 3, 4], | |
[6, 9, 4, 5, 3, 7, 2, 1, 8], | |
[1, 2, 3, 8, 4, 9, 7, 5, 6], | |
[4, 5, 1, 9, 7, 3, 6, 8, 2], | |
[3, 6, 7, 1, 8, 2, 5, 4, 9], | |
[2, 8, 9, 4, 5, 6, 1, 7, 3], | |
[5, 3, 6, 7, 9, 8, 4, 2, 1], | |
[9, 4, 8, 2, 1, 5, 3, 6, 7], | |
[7, 1, 2, 3, 6, 4, 8, 9, 5]] | |
check = lambda sudoku: all([set(range(1,10))-set([sudoku[y+i][x+j] for i in (-1,0,1) for j in (-1,0,1)])==set() for y in range(1,9,3) for x in range(1,9,3)]+[set(range(1,10))-set(i)==set() for i in sudoku+list(zip(*sudoku))]) | |
print(check(sudoku)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment