solution.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import sudoku as sdk
  2. def naked_twins(values):
  3. """
  4. Eliminate values using the naked twins strategy.
  5. Args:
  6. values(dict): a dictionary of the form
  7. {'box_name': '123456789', ...}
  8. Returns:
  9. the values dictionary with the naked twins eliminated from peers.
  10. """
  11. sudoku = sdk.Sudoku(values, partial=True)
  12. sudoku.naked_twins()
  13. return sudoku.values
  14. def solve(grid):
  15. """
  16. Find the solution to a Sudoku grid.
  17. Args:
  18. grid(string): a string representing a sudoku grid.
  19. Example: '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3'
  20. Returns:
  21. The dictionary representation of the final sudoku grid. False if no solution exists.
  22. """
  23. diagonal_sudoku = sdk.Sudoku(grid, diag=True)
  24. diagonal_sudoku.search()
  25. diagonal_sudoku.display()
  26. return diagonal_sudoku.values
  27. if __name__ == '__main__':
  28. diag_sudoku_grid = '9...6...7.6.971.4...........5.....3.41.....28.7.....6...........9.854.7.5...1...4'
  29. solve(diag_sudoku_grid)
  30. try:
  31. from visualize import visualize_assignments
  32. visualize_assignments(sdk.assignments)
  33. except SystemExit:
  34. pass
  35. except:
  36. print('Could not visualize your board due to a pygame issue. Not a problem! It is not a requirement.')