Apr-03-2018, 06:30 PM
I saw this code for solving sudoku using python on the internet, and I was reading it. I am a beginner python-er, so I don't quite understand some of these lines. If someone could please write an explanation for each line that would be great! Thanks!
Code:
Code:
import sys def same_row(i,j): return (i/9 == j/9) # Can someone explain how this math #proves that they are in the same? 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:]) # I really need help with this line which I don't get at all. if __name__ == '__main__': if len(sys.argv) == 2 and len(sys.argv[1]) == 81: r(sys.argv[1])# I also don't get the use of len(sys.argv) here else: print 'Usage: python sudoku.py puzzle' print ' where puzzle is an 81 character string representing the puzzle read left-to-right, top-to-bottom, and 0 is a blank'