Question
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:
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 okgrid() 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 4 5 6 7 8 9 2 3 4 5 6 7 8 9 1 3 4 5 6 7 8 9 1 2 4 5 6 7 8 9 1 2 3 5 6 7 8 9 1 2 3 4 6 7 8 9 1 2 3 4 5 7 8 9 1 2 3 4 5 6 8 9 1 2 3 4 5 6 7 9 1 2 3 4 5 6 7 8
Here is a solved version of the puzzle that you might want to try.
4 3 5 2 6 9 7 8 1 6 8 2 5 7 1 4 9 3 1 9 7 8 3 4 5 6 2 8 2 6 1 9 5 3 4 7 3 7 4 6 8 2 9 1 5 9 5 1 7 4 3 6 2 8 5 1 9 3 2 6 8 7 4 2 4 8 9 5 7 1 3 6 7 6 3 4 1 8 2 5 9
Here's a random puzzle.
9 3 8 5 9 7 2 4 1 6 7 6 5 4 3 1 8 2 7 9 6 5 2 1 4 3 8 6 3 9 2 1 4 8 7 5 4 6 2 1 7 9 5 3 8 3 5 8 9 6 2 4 1 7 8 3 2 9 7 4 1 5 6 7 1 2 5 4 6 8 3 9 6 2 5 4 1 8 7 9 3
Provided Code:
# 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()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started