Next / Previous / Contents / Shipman's homepage

13. Truths._permuter(): Generate all possible input sets

tracecase
# - - -   T r u t h s . _ p e r m u t e r

    def _permuter(self, n):
        '''Generate all possible permutations of inputs.

          [ n is a positive int ->
              generate all the possible lists of n bool values ]
        '''

We'll use a recursive algorithm. The basis case is n==1: for one variable, there are two possible lists, [False] and [True]. For n>1, for each possible of n-1 values, we yield two results: one for False appended to that value, and one for True appended to that value.

tracecase
        #-- 1
        # [ if n < 2 ->
        #     generate [False] and then [True]
        #   else ->
        #     generate all possible permutations of n-1 bool values,
        #     twice each, the first time with False appended and the
        #     second time with True appended ]
        if n < 2:
            yield [False]
            yield [True]
        else:
            for subPerm in self._permuter(n-1):
                yield subPerm+[False]
                yield subPerm+[True]