Next / Previous / Contents / Shipman's homepage

42. Puzzle.__uniqueWordTest(): Require that a word be used only once in the puzzle

kkck
# - - -   P u z z l e . _ _ u n i q u e W o r d T e s t

    def __uniqueWordTest ( self, slot ):
        '''Find slots with one choice, and remove that choice from others

          [ slot is a Slot in self ->
              if slot has only one choice ->
                remove that choice from all other slots
              else -> I ]
        '''
        #-- 1 --
        # [ slotChoiceList  :=  list of all current choices for slot ]
        slotChoiceList = [ word
            for word in self.__wordBank.genSlotChoices(slot) ]

If there are multiple choices (or no choices), we are done: no elimination can be performed. Otherwise, find the set of slots that have that word, and eliminate it from the choices for those slots. See Section 73, “WordBank.wordToSlots(): Which slots use a given word?” and Section 68, “WordBank.isNot(): Remove one word from a slot's list of choices”.

kkck
        #-- 2 --
        if len(slotChoiceList) != 1:
            return
        else:
            word = slotChoiceList[0]

        #-- 3 --
        # [ otherSlotList  :=  list of slots that use word,
        #       minus slot ]
        otherSlotList = self.__wordBank.wordToSlots ( word )
        otherSlotList.remove ( slot )

        #-- 4 --
        # [ self.__wordBank  :=  self.__wordBank - (choices for
        #      slots in otherSlotList that equal word ]
        for otherSlot in otherSlotList:
            if VERBOSE:
                print ( "*** Removing %s from %s because it goes in "
                    "%s." % (word, otherSlot, slot) )
            self.__wordBank.isNot ( otherSlot, word )