Question: Write a Python function powerSet() that takes in a finite list object AA and returns P(A)P(A), the power set of A. The output should be
Write a Python function powerSet() that takes in a finite list object AA and returns P(A)P(A), the power set of A. The output should be only in the form of list objects.
Note: Make sure you are familiar with some of Python's basic built-in list functions and operators, as they will make completing this problem far easier.
For example:
| Test | Result |
|---|---|
A = [0, 1, 2] output = set([frozenset(a) for a in powerSet(A)]) expected = [[], [0], [0, 1], [2], [1], [0, 2], [1, 2], [0, 1, 2]] expected = set([frozenset(a) for a in expected]) print (output==expected) | True |
A = ['a', 'b', 'c', 'd'] output = set([frozenset(a) for a in powerSet(A)]) expected = [[], ['a'], ['b'], ['a', 'b'], ['c'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c'], ['d'], ['a', 'd'], ['b', 'd'], ['a', 'b', 'd'], ['c', 'd'], ['a', 'c', 'd'], ['b', 'c', 'd'], ['a', 'b', 'c', 'd']] expected = set([frozenset(a) for a in expected]) print (output==expected) | True |
I will upvote! If code actually works in Python.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
