FP07 - Sudoku

(taken from Project Euler Problem 96)

No prerequisites. Available to all.

Su Doku (Japanese meaning number place) is the name given to a popular puzzle concept. Its origin is unclear, but credit must be attributed to Leonhard Euler who invented a similar, and much more difficult, puzzle idea called Latin Squares. The objective of Su Doku puzzles, however, is to replace the blanks (or zeros) in a 9 by 9 grid in such that each row, column, and 3 by 3 box contains each of the digits 1 to 9. Below is an example of a typical starting puzzle grid. Start:

0 0 3
9 0 0
0 0 1
0 2 0
3 0 5
8 0 6
6 0 0
0 0 1
4 0 0
0 0 8
7 0 0
0 0 6
1 0 2
0 0 0
7 0 8
9 0 0
0 0 8
2 0 0
0 0 2
8 0 0
0 0 5
6 0 9
2 0 3
0 1 0
5 0 0
0 0 9
3 0 0
A well constructed Su Doku puzzle has a unique solution and can be solved by logic, although it may be necessary to employ "guess and test" methods in order to eliminate options (there is much contested opinion over this). The complexity of the search determines the difficulty of the puzzle; the example above is considered easy because it can be solved by straight forward direct deduction. It must be emphasised however that the best methods for solving sudoku by hand are not necessarily the same as the best methods for a computer whose power is being able to do many operations very quickly.

Your main aim is to write two functions (which may also include further helper functions as you wish).

SolveSudoku(grid,layer)
doTests()

SolveSudoku should accept a single parameter defining your grid to solve. This will be a string, with 0s used to represent cells that need to be completed. The function should return an equivalent string, but of course, there should not be any 0s! The second parameter, layer, may be used to keep track of any recursion that you might use (if you choose not to use it, that's fine, but it must be part of the definition).

The function doTests() does not accept any inputs. It should be used to hold all your tests for the functions you have specified, and should return True if every test passes. This will enable us to check that the final code is working as expected simply by calling assert doTests().

You have also been provided with a function ViewGrid which will assist in displaying a grid, again provided as a string.

Planning

Set up the described functions. Do not write the code for them at this point. You must simply provide the def statements, doc string, and a return statement. You may also give any import commands that you expect to be appropriate.

Start to plan how you will construct these functions. Again, do not write the code, but at a higher level, what functions will your main algorithm need to call? Define those functions carefully in terms of inputs and outputs. (Of course, these may change when you come to program it later, that's fine, but the better you plan now, the less will have to change later.)

Both for the main functions and any sub-functions that you write, include appropriate pre- and post-conditions that verify the input and output. Write some tests that verify the correct operation of these functions. Remember that these tests will fail at the moment because you haven't written the functions!

Marks: 10
In [1]: