Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In python please Assignment In this assignment, you will determine if a puzzle has been solved based on the contents of the 9x9 grid. Complete

In python please

image text in transcribedimage text in transcribed

image text in transcribed

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 to9 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, 91 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 456789 2 34567 89 1 3 45678912 4 5678912 3 5 67891 2 3 4 6 7 8912 3 4 5 7 89 1 2 3 45 6 8912 345 6 7 91 2 3 4567 8 Here is a solved version of the puzzle that you might want to try 4 3 5 2 69 781 6 8 2571493 1 97 834 5 62 8 2 6195 3 4 7 3 7 46 8 2915 95174 3628 5 1 9 3 268 7 4 2 489571 3 6 7 6 3 418 2 5 9 1 # sudoku checker 2 # 1'n lazy. let's write something to check sudokus. 3 To be solved, each row, column, and 3x3 grid must be valid 4 # Valid means contains each of the values 1, 2, 3, 4, 5, 6, 7, 8, 9. 6 valid-[1,2,3,4,5,6,7,8,9] 9 def get_sudoku(filename): 10 11 Returns a two dimenional list structure containing the 9x9 sudoku 12 DO NOT CHANGE THIS CODE 13"" 14 grid [] 15 with open(filename, r) as file: 16 17 18 return grid 19 20 21 def okrows (sudoku): 22 "Returns true only if each of the rows in the sudoku is valid. 23 i.e each row contains numbers between 1-9""" for line in file: grid.append([int (i) for i in line.split(' ')]) # your code here 25 26 return True 27 28 29 def okcols(sudoku): 30"Returns true only if each of the columns in the sudoku is valid 31 i.e each column contains numbers between 1-9""" 32 # your code here 34 return True 35 36 37 def okgrid(sudoku,r,c): 38"Returns true if a 3x3 subgrid located from r,c to rt2,c+2 is valid." 39 # your code here 40 41 return True 42 43 44 def okgrids (sudoku): 45" 6 Returns true only if each of the nine 3x3 grids in the sudoku are valid 47 DO NOT CHANGE THIS CODE 48" 49 for r in range (e,9,3): 50 51 52 53 54 return True for c in range(0,9,3): ok okgrid(sudoku,r,c) if not ok: return False 56 57 def main): 58 59 Tells us if we have solved a sudoku 60 DO NOT CHANGE THIS CODE 61 "*" 62 #prompt the user for filename 63 filename -npt ( filename?n) 64 sudoku get_sudoku(filename) 65 solved [okrows (sudoku), okcols (sudoku), okgrids(sudoku)1 66 #all function acts like an "and" operator 67 #all(solved) gives you a boolean value of (okrows(sudoku) and okcols(sudoku) and okgrids(sudoku)) 68 print (solved,all(solved)) 69 70 71 if namemain: 72 main()

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

Recommended Textbook for

Beginning VB.NET Databases

Authors: Thearon Willis

1st Edition

1594864217, 978-1594864216

Students also viewed these Databases questions

Question

Consistently develop management talent.

Answered: 1 week ago

Question

Create a refreshed and common vision and values across Europe.

Answered: 1 week ago

Question

Provide the best employee relations environment.

Answered: 1 week ago