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

Here is a code, and this is 2D list sudoku. But I am not sure how to do it. def

ID: 3663951 • Letter: H

Question

Here is a code, and this is 2D list sudoku. But I am not sure how to do it.

def checkLst(lst):
    """returns True if lst (may represent row, column or a block) has the values 1 to 9"""
    #replace pass by the necessary code
    pass
       

def isValid(grid):
    """returns True if solution is valid and False otherwise"""

    #verify that every row has the numbers from 1 to 9
    #your code goes here

    #verify that every column has the numbers from 1 to 9
    #your code goes here

    #verify that every 3-by-3 box has the numbers from 1 to 9
    #Boxes will be processed in a left to right, top to bottom order
    startRow = 0 #row coordinate of starting cell in a 3-by-3 box
    startColumn = 0 #column coordinate of starting cell in a 3-by-3 box
    for boxNumber in range(0, 9):
        currentBox = []
        for row in range(startRow, startRow + 3):
            for column in range(startColumn, startColumn + 3):
                currentBox.append(grid[row][column])
        #display(currentBox)
        if checkLst(currentBox) == False:
            return False
        startColumn += 3 #time to move to the next box
        if startColumn == 9: #time to move to the next row of boxes
            startColumn = 0
            startRow += 3
           
    #if here, then solution must have passed all verification
    return True


def main():
    file_names_list = ["sudoku1.txt", "sudoku2.txt", "sudoku3.txt", "sudoku1b.txt", "sudoku2b.txt", "sudoku3b.txt"]
   
    for file_name in file_names_list:
        grid = [] #to store solution read from file
        f = open(file_name)
        for line in f:
            line = line.split()
            line = list(map(int, line)) # convert strings to integres
            grid.append(line)

        if isValid(grid):
            print("valid solution")
        else:
            print("invalid solution")
       

main()  

Explanation / Answer

*************Here is the way to do************

def main():
grid = readASolution()
if isValidGrid(grid):
print("Valid solution")
else:
print("Invalid solution")
def readASolution():
print("Enter a Sudoku puzzle solution:")
grid = []
for i in range(9):
line = input().strip().split()
grid.append([eval(x) for x in line])
return grid
def isValidGrid(grid):
for i in range(9):
if not is1To9(grid[i]):
return False
for j in range(9):
column = []
for i in range(9):
column.append(grid[i][j])
if not is1To9(column):
return False
for i in range(3):
for j in range(3):
k = 0
list = 9 * [0]
for row in range(i * 3, i * 3 + 3):
for column in range(j * 3, j * 3 + 3):
list[k] = grid[row][column]
k += 1
if not is1To9(list):
return False
return True
def is1To9(list):
temp = [x for x in list]
temp.sort()
return temp == [1, 2, 3, 4, 5, 6, 7, 8, 9]
main()

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