CSCI 233 Python Exercise 8

This lab continues to work with writing a class and using various basic Python features.

Checking a Sudoku

A completed Sudoku grid is a 9x9 grid filled with the digits 1-9 in such a way that each digit occurs exactly once in each row, each column, and each of the nine boxes outlined in bold, as in



















123456789









4 56789123









789123456


















2 34567891









567891234









891234567


















345678912









678912345









912345678


















Your goal is to produce a class and a program for reading in a Sudoku grid and checking whether it is a valid solution. At a minimum, you should print whether the grid is correct or not; it would be preferablle to print a message that specifically highlights at least one error in the grid.

Standardizing the vocabulary

Many Sudoku players are not computer programmers; so they expect to refer to the rows, columns, and boxes as numbered 1 through 9. The boxes are numbered left to right, top to bottom:







1 2 3






4 5 6






7 8 9






General design

We worked through the overall design during Wednesday’s class. Some of the ideas we noted:

What can be wrong?

There are different kinds of mistakes that we might find as we read or check a grid. One possibility is that the input is malformed: some rows are not the right length, we reach end-of-input before reading the right number of rows, or something like that. There could also be characters (non-digits or zeros). The more interesting errors are those in a properly shaped grid full of the right digits; these would usually be expressed in terms of there being duplicates in one (or more) of the sets.

Think about how to produce messages that would be useful to a person working on a puzzle. You have some freedom here; I suggest that you start simple and make it better as time permits.

Turning it in

Polish your portion of the program in sudoku.py. Write your reflections in sudoku.txt.

Turn in what you have at the end of the lab today; be sure that your notes in sudoku.txt explain where you are and what you still have to do.

If you don’t finish today, turn in a completed program before 5:00 p.m. on Wednesday, March 17.

To turn in your files, use the command

/cslab/class/csci233/bin/handin P8 filenames