Sudoku in Python

A friend of mine asked me to write a Sudoku module in Python so he could compare that to his C-implementation, and to see how Python works.

I wrote a simple class for 9x9 Sudokus which includes a brute force solver, that just iterates through all possible combinations until it either finds a solution or aborts when no solution is possible.

Simple usage:

>>> import sudoku
>>> s = sudoku.Sudoku()
>>> s.setRandomFields() # generate a task and set some fields
>>> print s
|8|_|6| |_|7|_| |_|_|_|
|_|1|_| |_|_|_| |8|6|3|
|9|_|_| |_|_|_| |_|_|1|

|_|_|_| |_|_|_| |_|_|_|
|_|_|_| |_|_|1| |4|3|8|
|4|5|1| |_|_|_| |_|2|6|

|_|_|_| |_|4|_| |_|1|_|
|_|8|_| |3|_|6| |_|_|_|
|_|_|_| |9|_|_| |_|_|_|
>>> s.solveBruteForce() # might take a looong time
>>> print s
|8|2|6| |1|7|3| |5|4|9|
|5|1|7| |2|9|4| |8|6|3|
|9|3|4| |6|8|5| |2|7|1|

|3|6|8| |4|2|7| |1|9|5|
|2|7|9| |5|6|1| |4|3|8|
|4|5|1| |8|3|9| |7|2|6|

|6|9|5| |7|4|8| |3|1|2|
|7|8|2| |3|1|6| |9|5|4|
|1|4|3| |9|5|2| |6|8|7|

There might be bugs. I release it under the MIT license.

See also Sudoku in TCL.

AttachmentSize
sudoku.py.txt6.13 KB

where are your friend's

where are your friend's C-codes?