Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5 stars for the first program that shows how to write a python program to check

ID: 3567040 • Letter: 5

Question

5 stars for the first program that shows how to write a python program to check for sudoku solutions. Follow the structure of the provided code. Thanks.

Needs to check for vailidty of each Row, Column and Grid individually.

''' Determines whether a Sudoku solution is valid. '''

def check_rows(soln):

''' Checks each row in the solution. Returns True if each row is valid and False otherwise.'''

pass

def check_cols(soln):

''' Checks each column in the solution. Returns True if each column is valid and False otherwise.'''

pass

def check_blocks(soln):

''' Checks each 3 x 3 block in the solution. Returns True if each block is valid and False otherwise.'''

pass

def valid_soln(soln):

''' Returns True if a Sudoku solution is valid and False otherwise.'''

''' Check if rows are valid. '''

if check_rows(soln) == False:

return False

''' Check if columns are valid. '''

if check_cols(soln) == False:

return False

''' Check if 3x3 submatrices (or blocks) are valid. '''

if check_blocks(soln) == False:

return False

''' If all checks pass, then the solution is valid. '''

return True

def main():

''' The checking process begins here. '''

file_name = raw_input('Enter filename: ')

file_data = open(file_name).readlines()

'''sudoku_soln is a nested listed of size 9. Each entry in sudoku_soln is a list of size 9. Each list is initialized to zeroes. Draw a picture of the sudoku_soln data structure before you write the remaining code.'''

sudoku_soln = []

for i in range(9):

sudoku_soln += [[0] * 9]

'''sudoku_soln is populated with data from the solution file. We will learn about the enumerate built-in function on Tuesday, Nov. 3rd. You do not need to use enumerate in any of the code you write for this assignment.'''

for i, line in enumerate(file_data):

for j, value in enumerate(line.strip()):

sudoku_soln[i][j] = int(value)

''' Check and print results. '''

if valid_soln(sudoku_soln):

print 'Valid solution.'

else:

print 'Invalid solution.'

main()

Explanation / Answer

correct = [[1,3,2],
[2,1,3],
[3,2,1]]

incorrect = [[1,2,4,3],
[2,3,1,3],
[3,1,2,3],
[4,2,2,4]]


def check_sudoku(game):
n = len(game)
if n < 1:
return False
for i in range(0, n):
horizontal = []
vertical = []
for k in range(0, n):
#vertical check
if game[k][i] in vertical:
return False
vertical.append(game[k][i])

if game[i][k] in horizontal:
return False
horizontal.append(game[i][k])
return True

print check_sudoku(correct)
print check_sudoku(incorrect)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote