Learning Scientific Programming with Python (2nd edition)

P4.3.5: Power sets

Question P4.3.5

The power set of a set $S$, $P(S)$, is the set of all subsets of S, including the empty set and $S$ itself. For example,

P((1,2,3)) = (), (1), (2), (3), (1,2), (1,3), (2,3), (1,2,3)

Write a Python program which uses a generator to return the power set of a given set.

Hints: convert your set into an ordered sequence such as a tuple. For each item in this sequence return the power set formed from all subsequent items, inclusive and exclusive of the chosen item. Don't forget to convert the tuples back to sets after you're done.