Power set of a sequence

Run without arguments
mail@pastecode.io avatar
unknown
python
2 years ago
393 B
6
Indexable
Never
# Define powerset of *any* sequence.
def powerset(sequence):
  sequence = list(sequence)
  subsets = []
  for s in range(2**len(sequence)):
    subset = []
    for k in range(len(sequence)):            
      if s & 1<<k:
        subset.append(sequence[k])
    subsets.append(set(subset))        
  return subsets

# Execute powerset.
if __name__ == "__main__":
    print(powerset({0, 1, 2}))