Next / Previous / Contents / Shipman's homepage

47. Puzzle.__assumeChoice(): Try out an assumption

kkck
# - - -   P u z z l e . _ _ a s s u m e C h o i c e

    def __assumeChoice ( self, thisSlot, choice, nextSlot ):
        '''Generate solutions assuming thisSlot==choice

          [ (thisSlot is a slot in self) and
            (nextSlot is the successor of thisSlot) and
            (choice is a Word that is NOT a choice for thisSlot in
            self.__wordBank) and
            (all slots preceding thisSlot have a single choice) ->
              generate solutions to self assuming thisSlot==choice ]
        '''

This method calls the recursive solver (Section 44, “Puzzle.__recurSolver(): Recursive solver”) to generate any solutions that result from assuming that thisSlot should contain choice. It assumes that the caller has already removed choice from self.__wordBank's choices for thisSlot.

If we assume that choice is the right word for thisSlot, then we can remove that word from all the other slots for which it is a choice. However, we need to put that word back into the set of choices for each of those other slots before returning.

kkck
        #-- 1 --
        # [ otherSlotList  :=  slots in self for which self.__wordBank
        #       shows (choice) as one of their choices ]
        otherSlotList = self.__wordBank.wordToSlots ( choice )

        #-- 2 --
        # [ self.__wordBank  :=  self.__wordBank + (choice (choice)
        #       for thisSlot) - (choice (choice) for all slots in
        #       otherSlotList ]
        self.__wordBank.mayBe ( thisSlot, choice )
        for otherSlot in otherSlotList:
            self.__wordBank.isNot ( otherSlot, choice )

        #-- 3 --
        # [ generate solutions assuming slots preceding nextSlot
        #   are unchanged ]
        for solution in self.__recurSolver ( nextSlot ):
            yield solution

        #-- 4 --
        # [ self.__wordBank  :=  self.__wordBank - (choice (choice)
        #       for thisSlot) + (choice (choice) for all slots in
        #       otherSlotList ]
        for otherSlot in otherSlotList:
            self.__wordBank.mayBe ( otherSlot, choice )
        self.__wordBank.isNot ( thisSlot, choice )