This just needed a few corrections to get running:
* solve_cover should simply be solve
* The solution is a list with elements in the form (digit, x location, y location), zero-indexed. So I changed the ending some:
out = [['0','0','0','0','0','0','0','0','0','\n'],
['0','0','0','0','0','0','0','0','0','\n'],
['0','0','0','0','0','0','0','0','0','\n'],
['0','0','0','0','0','0','0','0','0','\n'],
['0','0','0','0','0','0','0','0','0','\n'],
['0','0','0','0','0','0','0','0','0','\n'],
['0','0','0','0','0','0','0','0','0','\n'],
['0','0','0','0','0','0','0','0','0','\n'],
['0','0','0','0','0','0','0','0','0']]
for d, x, y in solution:
out[x][y] = str(d)
print(''.join([a for b in out for a in b])) # Flatten 2d array
There's probably a more elegant/pythonic way to do each code block, but this will get you a valid solution matrix: (Format lines were added during posting.)
It is a bit interesting seeing the similarities and differences between the posted article and Norvig's Sudoku solver[1]. I've always appreciated how Norvig is concise and still has intermediate representations for debugging (e.g. shows all of the possible digits for a cell that hasn't yet been solved). The article's solver is MUCH faster in non-exact cases: The "multiple solution" example from Norvig (which Norvig states takes 3 minutes to solve) is done in 10 ms using this solver (plus, it finds a different valid solution), and the "impossible solution" (which fails after 24 minutes) gives a StopIteration after 90 ms.
* solve_cover should simply be solve
* The solution is a list with elements in the form (digit, x location, y location), zero-indexed. So I changed the ending some:
There's probably a more elegant/pythonic way to do each code block, but this will get you a valid solution matrix: (Format lines were added during posting.) It is a bit interesting seeing the similarities and differences between the posted article and Norvig's Sudoku solver[1]. I've always appreciated how Norvig is concise and still has intermediate representations for debugging (e.g. shows all of the possible digits for a cell that hasn't yet been solved). The article's solver is MUCH faster in non-exact cases: The "multiple solution" example from Norvig (which Norvig states takes 3 minutes to solve) is done in 10 ms using this solver (plus, it finds a different valid solution), and the "impossible solution" (which fails after 24 minutes) gives a StopIteration after 90 ms.[1] https://norvig.com/sudoku.html