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

In Program 1, how can we program in python to check each block in Sudoku? The co

ID: 3693763 • Letter: I

Question

In Program 1, how can we program in python to check each block in Sudoku?

The code below is the sample code: q1.py

# Determines whether a Sudoku solution is valid.
def valid_rows(soln):
''' Returns True if each row is valid
and False otherwise.

'''
rows= [sum(t) for t in soln]

#print(rows)
  
total=rows.count(45)
  
if (total==9):
return True
else:
return False
  
  

def valid_cols(soln):
''' Returns True if each column
is valid and False otherwise.
'''
  
i = 0
length= len(soln)
  
for j in range(0,length):
total= 0
for l in range(0,length):
total = total + soln[l][j]
if total== 45:
i+=1
if i == 9:
return True
else:
return False

def valid_blocks(soln):
''' Checks each 3 x 3 block in the puzzle.
Returns True if each block is valid
and False otherwise.
'''
  

def valid_soln(soln):
''' Returns True if a Sudoku solution is valid and False
otherwise.
'''
  
# Check if rows are valid.
if not valid_rows(soln):
return False

# Check if columns are valid.
if not valid_cols(soln):
return False

# Check if 3x3 submatrices (or blocks) are valid.
if not valid_blocks(soln):
return False
  
# If all checks pass, then the solution is valid.
return True


def main():
  
''' The checking process begins here. '''

file_name = input('Enter filename: ')
  
input_file = open(file_name)
file_data = input_file.readlines()

input_file.close()
  
# sudoku_soln is populated with the data
# from the solution file. sudoku_soln is a
# tuple of tuples, where each tuple is of size
# 9. Draw a picture of sudoku_soln before you
# try to write the remaining code.
  
# Also, when adding values to a tuple, if we
# are only add one tuple, we must use a comma
# at the end (see Lines A & B).

sudoku_soln = ()
for line in file_data:
row = ()
for value in line.strip():
row += (int(value),) # Line A
sudoku_soln += (row,) # Line B

# Check and print results.
print()
if valid_soln(sudoku_soln):
print('Valid solution')
else:
print('Invalid solution')


main()

3 Lab Questions 1. Sudoku solution checker. Write a program (called q1.py) that determines whether a Sudoku solution is valid or invalid. In Sudoku, the objective is to fill a 9x9 grid with digits so that each row, each column, and each of the nine 3×3 blocks contains all of the digits from 1 to 9 (see Figure 1 below). 54 689 3 2 1 7 4 6 8 9 3 2 392174 6 8 5 8175 2 69 34 45 861 23 79 67 395 8421 Figure 1: Sudoku solution The solution will be stored in a file containing 9 lines, where each line represents the solution for a row. For example, consider the file soln1.txt below, which contains the solution for the puzzle shown in Figure 1. soln1.txt 1546893217 2392174685 3817526934 4 769341852 5234785196 6185269743 7458612379 8 921437568 9 673958421 Since solnl.txt represents a valid solution, your program will output "Valid solution." If a solution is invalid, your program will output "Invalid solution." . The provided Your objective. Sample code has been provided for you in ql.py code (i) asks the user for a file, (ii) stores the data from the file in a nested tuple of tuples, and (iii) prints the results. Your job is to write the remaining pieces of the code to verify the solution by checking the nine rows, nine columns, and nine 3x3 blocks. Thus, the functions you will write are valid_rows (soln), valid_cols (soln), and valid_blocks (soln), respectively.

Explanation / Answer

Ans;

This is my solution:

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