Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# sudoku checker # I'm lazy. let's write something to check sudokus. # To be solved, each row, column, and 3x3 grid must be valid.

image text in transcribedimage text in transcribedimage text in transcribed

# sudoku checker # I'm lazy. let's write something to check sudokus. # To be solved, each row, column, and 3x3 grid must be valid. # Valid means contains each of the values 1, 2, 3, 4, 5, 6, 7, 8, 9.

valid = [1,2,3,4,5,6,7,8,9]

def get_sudoku(filename): """ Returns a two dimenional list structure containing the 9x9 sudoku. DO NOT CHANGE THIS CODE """ grid = [] with open(filename,'r') as file: for line in file: grid.append([int(i) for i in line.split(' ')]) return grid

def okrows(sudoku): """Returns true only if each of the rows in the sudoku is valid. i.e each row contains numbers between 1-9""" # your code here return True

def okcols(sudoku): """Returns true only if each of the columns in the sudoku is valid. i.e each column contains numbers between 1-9""" # your code here return True

def okgrid(sudoku,r,c): """Returns true if a 3x3 subgrid located from r,c to r+2,c+2 is valid.""" # your code here return True

def okgrids(sudoku): """ Returns true only if each of the nine 3x3 grids in the sudoku are valid. DO NOT CHANGE THIS CODE """ for r in range(0,9,3): for c in range(0,9,3): ok = okgrid(sudoku,r,c) if not ok: return False return True

def main(): """ Tells us if we have solved a sudoku. DO NOT CHANGE THIS CODE """ #prompt the user for filename filename = input('filename? ') sudoku = get_sudoku(filename) solved = [okrows(sudoku), okcols(sudoku), okgrids(sudoku)] #all function acts like an 'and' operator #all(solved) gives you a boolean value of - (okrows(sudoku) and okcols(sudoku) and okgrids(sudoku)) print(solved,all(solved))

if __name__ == '__main__': main()

Assignment In this assignment, you will determine if a puzzle has been solved based on the contents of the 9x9 grid. Complete the following code to read the 9x9 grid of numbers from a file determine if each row, column, and subgrid contains all of the digits from 1 to 9. print the result Remember that we test your functions, not just the output of the program. Implement the okgrid0 function last, after everything else works. It is only worth one point and is not included in the output test comparisons. You can leave it as is to earn 9/10 points on the assignment Hint Collect the values from a row, column, or subgrid in a list, sort the list, and compare the list with valid defined at the top of the program. valid [1, 2, 3, 4, 5, 6, 7,8, 9] Example Input The input files will contain 9 lines with 9 numbers in each line separated by spaces. This one would be particularly good for testing your rows and columns. For example 1 2 3 45 6 7 89 2 3 45 67 8 9 1 3 4 5 678912 4 5 6 78 9 1 2 3 5 6 7 891 2 34 6 78912 3 4 5 7 89 1 2 3 45 6 8 9 1 2 3 4 5 6 7 91 2 3 45678 Here is a solved version of the puzzle that you might want to try 4 3 5 269 78 1 6 8 25 7 1 49 3 1 97 8 3 45 6 2 8 261 9 5 3 47 3 7 4 6 8 2 915 95 1 7436 2 8 5 1 9 3 26 87 2 4 89571 36 7 6 3 418 2 5 9 Here's a random puzzle 9 3 8 5 97 2 4 1 6 7 6 5 4 31 8 2 7 96 5 2 1 4 3 8 6 3 9 214 87 5 4 6 2 1 79 5 3 8 8 3 2974 1 5 6 7 1 2 5 4 6 8 3 9 6 2 5 4 1 8 79 3 Example Output The output show whether the puzzle is solved, and whether the row, column, and grids were individually valid # sample output for the first example [True, True, False] False # sample output for the second example [True, True, True] True # sample output for the third example [False, False, False] False

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions