Using Python to compile 1 General presentation A sudoku grid consists of 9 lines
ID: 3595538 • Letter: U
Question
Using Python to compile
1 General presentation A sudoku grid consists of 9 lines and 9 columns, making up 81 cells, that are grouped in nine 3x3 boxes. In a sudoku puzzle, some but not all of the cells already contain digits between 1 and 9. Here is an example of a sudoku puzzle. 6 6 Solving a sudoku puzzle means completing the grid so that each digit from 1 to 9 occurs once and only once in every row, once and only one in every column, and once and only once in every box. For instance, the previous puzzle has the following solutionExplanation / Answer
import sys
def sameRow(i,j): return (i/9 == j/9)
def sameCol(i,j): return (i-j) % 9 == 0
def sameBlock(i,j): return (i/27 == j/27 and i%9/3 == j%9/3)
def solve(a):
i = a.find('0')
if i == -1:
sys.exit(a)
excluded_numbers = set()
for j in range(81):
if sameRow(i,j) or sameCol(i,j) or sameBlock(i,j):
excluded_numbers.add(a[j])
for m in '123456789':
if m not in excluded_numbers:
r(a[:i]+m+a[i+1:])
return a;
if __name__ == '__main__':
if len(sys.argv) == 2 and len(sys.argv[1]) == 81:
solve(sys.argv[1])
'''
This program takes a 81 length string of numbers read from left
to right and top to bottom. 0 is the blank space.
'''
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.