Next / Previous / Contents / Shipman's homepage

72. WordBank.cellChoices(): What characters are choices at a given coordinate?

This method has two applications:

kkck
# - - -   W o r d B a n k . c e l l C h o i c e s

    def cellChoices ( self, coord ):
        '''What characters are possible choices at a given location?
        '''

The first order of business is to determine whether there is a cell at this location, and return a space if not.

kkck
        #-- 1 --
        # [ if there is a cell at coord in self.puzzle ->
        #     cell  :=  the Cell instance at coord
        #   else -> return a space character ]
        cell = self.puzzle.whatCell ( coord )
        if cell is None:
            return ' '

So that this method can be used to echo the puzzle layout before its slot choices have been added, if self.__totalChoices is zero, we can simply display the text of the cell, and we're done.

kkck
        #-- 2 --
        if self.__totalChoices == 0:
            return cell.text

However, because this method is also used during the solution process, in that case we'll want to show the solution for cells that have been solved.

For each slot that intersects this cell (there may be either one or two), there is a set of choices for the letter that occurs at this position. However, when there is an intersection, the same letter must be used from each choice. Hence, the result we want is the intersection of those sets. The set.__iand__ operator is the method in the set type that implements the “&” operator, set intersection.

For the method that finds the characters that are in choices for a particular slot, see Section 71, “WordBank.slotCharChoices(): What letters are choices at a given position?”. For the method that generates all the slots that contain a given cell, see Section 51, “Cell.genSlots(): Generate the slots intersecting this cell”.

kkck
        #-- 3 --
        return reduce ( set.__and__,
            [ self.slotCharChoices ( slot, coord )
              for slot in cell.genSlots() ] )