This section describes the mandatory part of the project. The next section describes the optional part.
Below are a number of completed Sudoku puzzles. Three are correct. Three are incorrect. (I won't tell you which are which. ) Write me a program that distinguishes those that are solved correctly from those that are not. Print your conclusion. Like this: "Table 1 is correct (or incorrect). Table 2 is ... ". Etc.
How will you get the data from below into your program? Follow this link to a CodeHS project. The data is contained in a file named "sudoku_puzzles_solved.txt". (You can also find the data file at GitHub.) I've generously written a function for you that reads the lines in that file into a list. Of course that's only a small bit of the work you'll have to do. I suggest that you convert the list of strings generated by the function I provided into multiple 2D integer lists, one for each puzzle. For instance, the list for Table 1 will begin [[8, 4, 5, 6, 3, 2, 1, 7, 9], [7, 3, 2, 9, 1, 8, 6, 5, 4], ... ].
Note as well that if you store a table as a 2D array (and that does seem the best way to store it), you'll have to double index to pull out a particular integer. Like this:
>>> L = [[8, 4, 5, 6, 3, 2, 1, 7, 9], [7, 3, 2, 9, 1, 8, 6, 5, 4]]
>>> L[0][0]
8
>>> L[0][1]
4
>>> L[1][0]
7
>>> L[1][1]
3
On the day the project is due, I'll give you another file of completed Sudoku puzzles, formatted precisely as you found in the CodeHS link, and have you tell me which are solved correctly and which are not.
No doubt you'll write many functions. But one will be the master function - the only function that's called, the function that orchestrates the execution of all the others. Call this master function find_solved_sudoku or some such. It will have just one parameter, the name of the text file from which the program will read. The only line of code that won't be contained in some function or other is find_solved_sudoku(file_name). When you hit the run button, it will call find_solved_sudoku and that function will take over from there.
A final word. Try to make each of your functions single-task. Moreover, think carefully before you begin to code about what tasks need to be carried out; and as you write your code, aim to place each task in its own function. One central task, of course, is to determine whether a sequence of 9 integers contains all of 1 through 9. I'd certainly make that it's own function. So here's how you might structure your code:
A function to read in the tables. (Provided in the link above.)
A function to take that raw data and convert it into 2D lists of integers.
A function to pull sequences of 9 integers from those 2D lists - a sequence for each row, for each column and for 3-by-3 blocks. Perhaps this should be 3 functions - one to pull a row, one to pull a column, and one to pull a 3-by-3 block. I imagine that the 9 integers will be elements of a list.
A function to determine whether a given list of integers contains all and only the integers 1 - 9.
The master function (described above) that I suggested you call find_solved_sudoku.
Let me now describe the optional part of the project. This will be extra credit.
Write me a program that takes an unsolved Sudoku puzzle and solves it. The project I began for you contains a file named "sudoku_puzzles_unsolved.txt". Its contents are ten unsolved puzzles. (This file is also found at GitHub.) Note that empty positions are marked with a 0. Your task then is to convert all those 0's to the appropriate integers in the range from 1 - 9. Read in the contents of the file and solve the puzzles.
You may assume that each puzzle has precisely one correct solution.
On the day of the quiz, I'll provide a file with different unsolved puzzles formatted is precisely the same way.
Here's a bit of the file provided:
Table 1
003020600
900305001
001806400
008102900
700000008
006708200
002609500
800203009
005010300
Table 2
200080300
060070084
030500209
000105408
000000000
402706000
301007040
720040060
004010003
Here's how my program showed the solutions to these two unsolved puzzles. I suggest that you do something similar.
Table 1
4 8 3 | 9 2 1 | 6 5 7
9 6 7 | 3 4 5 | 8 2 1
2 5 1 | 8 7 6 | 4 9 3
-----------------------
5 4 8 | 1 3 2 | 9 7 6
7 2 9 | 5 6 4 | 1 3 8
1 3 6 | 7 9 8 | 2 4 5
-----------------------
3 7 2 | 6 8 9 | 5 1 4
8 1 4 | 2 5 3 | 7 6 9
6 9 5 | 4 1 7 | 3 8 2
Table 2
2 4 5 | 9 8 1 | 3 7 6
1 6 9 | 2 7 3 | 5 8 4
8 3 7 | 5 6 4 | 2 1 9
-----------------------
9 7 6 | 1 2 5 | 4 3 8
5 1 3 | 4 9 8 | 6 2 7
4 8 2 | 7 3 6 | 9 5 1
-----------------------
3 9 1 | 6 5 7 | 8 4 2
7 2 8 | 3 4 9 | 1 6 5
6 5 4 | 8 1 2 | 7 9 3