Next / Previous / Contents / Shipman's homepage

43. Puzzle.__uniqueSlotTest(): Find words that are choices for only one slot

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

    def __uniqueSlotTest ( self, word ):
        '''Find cases where only one slot can be a given word.

          [ word is a Word in self.__wordBank ->
              if word occurs in only one slot's choices ->
                 self.__wordBank  :=  self.__wordBank - (choices
                     for slot other than word) - (choices for
                     that word in all slots)
              else -> I ]
        '''

First we build a list of all the slots that use word, using Section 73, “WordBank.wordToSlots(): Which slots use a given word?”. If this list has only one element, then that slot is the one that should be filled with that word, and we can eliminate that word from all the other slot choices in the puzzle.

kkck
        #-- 1 --
        # [ slotList  :=  list of all slots for which word is
        #       currently a choice ]
        slotList = self.__wordBank.wordToSlots ( word )

        #-- 2 --
        if len(slotList) != 1:
            return
        else:
            slot = slotList[0]

First we have to eliminate the other choices for slot; see Section 70, “WordBank.genSlotChoices(): What words are choices for a slot?” and Section 68, “WordBank.isNot(): Remove one word from a slot's list of choices”.

kkck
        #-- 3 --
        # [ otherWordList  :=  choices for slot from self.__wordBank ]
        otherWordList = [ word
            for otherWord in self.__wordBank.genSlotChoices ( slot ) ]

        #-- 4 --
        # [ self.__wordBank  :=  self.__wordBank - (choices for slot
        #       in otherWordList that do not equal word) ]
        for otherWord in otherWordList:
            if otherWord != word:
                self.__wordBank.isNot ( slot, otherWord )

Since word is the only choice for slot, we can now eliminate that word from all other slots.

kkck
        #-- 5 --
        # [ self.__wordBank  :=  self.__wordBank - (choices for
        #       slots other than (slot) that equal (word) ]
        for otherSlot in self.__slotMap.values():
            if otherSlot is not slot:
                self.__wordBank.isNot ( otherSlot, word )