[Edu-sig] re: Algorithm help Subsets

Arthur Siegel ajs@ix.netcom.com
Sat, 2 Mar 2002 22:48:36 -0500


Danny writes - 

>>Here is a recursive definition of the subsets function:

Thanks.  I *almost* understand it - though still having trouble
wrapping my head around recursive stuff.

Similarly an alogorithm I am using to get the permutations of a list
excluding case were list = list.reverse():

def perms(source,done,current=[]):
       if done == len(source):
          if current[0] < current[-1]:
             P.append(current)
       else:
          for i in source:
             if i not in current:
                 perms(source,done+1,current+[i])

I got it from a python-list discussion - but only sort of follow it.

Not above using algorithms I don't fully understand.

Putting your alogorithm into 'prodiction' with a small amendment:

    case1 = addHeads(sequence.pop(0), subsets(sequence, n-1))
    case2 = subsets(sequence, n)

thinking that I am saving 2 slice (sequence[1:]) operations at no cost.

Thanks again.

Art