Advertisement
aseeon

Untitled

Mar 15th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. def solve_sudoku(sudoku: list) -> list or None:
  2.     """
  3.    Function which solves sudoku
  4.    :param sudoku: 2d list that represents sudoku, empty spaces should be marked as 0
  5.    :return: returns solution or None
  6.    """
  7.     solved = sudoku.copy()
  8.     empty_spaces = find_empty(sudoku)
  9.     counter = 0
  10.     correct = False
  11.     while 0 <= counter < len(empty_spaces):
  12.         i, j = empty_spaces[counter]
  13.         for tested_value in range(solved[i][j] + 1, 10):
  14.             correct = is_correct(tested_value, solved, (i, j))
  15.             if correct:
  16.                 solved[i][j] = tested_value
  17.                 break
  18.         if correct:
  19.             counter += 1
  20.         else:
  21.             solved[i][j] = 0
  22.             counter -= 1
  23.  
  24.     if correct:
  25.         return solved
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement