Next / Previous / Contents / Shipman's homepage

5.7. SudokuSolver.get(): Fetch one value from the board

This method retrieves the contents of one cell as an integer.

sudosolver.py
# - - -   S u d o k u S o l v e r . g e t   - - -

    def get ( self, r, c ):
        """Get the cell at row r, column c.
        """

First we check the row index r and column index c, and raise KeyError if either is out of bounds.

sudosolver.py
        #-- 1 --
        # [ if r is in [0,MAT_L) ->
        #     I
        #   else -> raise KeyError ]
        if  not ( 0 <= r < MAT_L ):
            raise KeyError, ( "SudokuSolver.get(): Bad row "
                "index, %d." % r )

        #-- 2 --
        # [ if c is in [0,MAT_L) ->
        #     I
        #   else -> raise KeyError ]
        if  not ( 0 <= c < MAT_L ):
            raise KeyError, ( "SudokuSolver.get(): Bad column "
                "index, %d." % c )

To convert a row and column number to an index in the self.__board structure, we use Section 5.8, “SudokuSolver.__rowColToX(): Convert row and column to board position”.

sudosolver.py
        #-- 3 --
        # [ x  :=  index in self.__board corresponding to row r,
        #          column c ]
        x  =  self.__rowColToX ( r, c )

Finally, return the cell's value.

sudosolver.py
        #-- 4 --
        return  self.__board[x]

For trace table verification, see Section 7.3, “Trace table: SudokuSolver.get().