Guest User

Untitled

a guest
May 16th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. from __future__ import print_function, division
  2.  
  3. def check_sudoku(arr):
  4. # 9 rows, 9 columns and 9 sub squares
  5. # Each of them need to have all the digits
  6. # Not using bit shifting
  7. alignments = [987654321] * 27
  8.  
  9. for i, row in enumerate(arr):
  10. for j, x in enumerate(row):
  11. if x == 0:
  12. return False
  13. n = x * pow(10, x - 1)
  14. alignments[i] -= n
  15. alignments[9 + j] -= n
  16. alignments[18 + i // 3 * 3 + j // 3] -= n
  17. return sum(alignments) == 0
  18.  
  19.  
  20. test_arr = [[5, 3, 4, 6, 7, 8, 9, 1, 2], [6, 7, 2, 1, 9, 5, 3, 4, 8], [1, 9, 8, 3, 4, 2, 5, 6, 7],
  21. [8, 5, 9, 7, 6, 1, 4, 2, 3], [4, 2, 6, 8, 5,
  22. 3, 7, 9, 1], [7, 1, 3, 9, 2, 4, 8, 5, 6],
  23. [9, 6, 1, 5, 3, 7, 2, 8, 4], [2, 8, 7, 4, 1, 9, 6, 3, 5], [3, 4, 5, 2, 8, 6, 1, 7, 9]]
  24. check_sudoku(test_arr)
Add Comment
Please, Sign In to add comment