I tried this Sudoku solver written in Python https://freepythontips.wordpress.com/2013/09/01/sudoku-solver-in-python/ The code works fine. This is the full code:
import sys
def same_row(i,j): return (i/9 == j/9)
def same_col(i,j): return (i-j) % 9 == 0
def same_block(i,j): return (i/27 == j/27 and i%9/3 == j%9/3)
def r(a):
i = a.find('0')
if i == -1:
sys.exit(a)
excluded_numbers = set()
for j in range(81):
if same_row(i,j) or same_col(i,j) or same_block(i,j):
excluded_numbers.add(a[j])
for m in '123456789':
if m not in excluded_numbers:
r(a[:i]+m+a[i+1:])
r('100070030830600000002900608600004907090000050307500004203009100000002043040080009')
I dont understand this loop:
for m in '123456789':
if m not in excluded_numbers:
r(a[:i]+m+a[i+1:])
Please someone explain the algorithm of this code. Thanks
Edit: I was asking about logic of that loop and now I understand the logic and I checked Peter Novig's Sudoku solver.