Question: In python, implement powerset and accompanying functions in the giving code. The docstrings of each function elaborate on the constraints of each fn. Make sure
In python, implement powerset and accompanying functions in the giving code. The docstrings of each function elaborate on the constraints of each fn. Make sure to test your code.
Python code:
"""
Since elements of Python sets must be immutable,
a Python set cannot contain Python sets.
For example, a powerset cannot be represented using Python sets.
So we will simulate a set using lists.
For example, [[1,2], [1,3]] represents the set {{1,2}, {1,3}}.
This works, since lists do not have the immutability constraint.
"""
def randomSet (n, up):
"""Build a random 'set' of integers.
Be sure to guarantee the key property of a set: no duplicates.
>>> randomSet(4, 10)
[5, 3, 2, 8]
Params:
n (int): # of integers in the set
up (int): upperbound
Returns: (list) random 'set' of n integers between 0 and up, inclusive
"""
pass
def powerset (A):
"""Build the powerset of A.
>>> powerSet ([1,2])
[[], [1], [2], [1,2]]
Params: A (list): set of integers, represented internally as a list
Returns: (list) powerset of A, represented internally as a list
"""
pass
def printSet (L):
"""Print a 'set', using standard set notation.
To restore the correct interpretation, this function prints the 'set'
in true set notation.
Hint: you can print without adding a newline (see print documentation)
>>> printSet ([[1,2], [1,3]]
{{1,2}, {1,3}}
Params: A (list): set of integers
"""
pass
# an example of a test call
A = randomSet (5, 100)
# print (A)
print ("The powerset of ")
printSet (A)
print ("is")
B = powerset(A)
printSet (B)
print ("of size " + str(len(B))) # sanity check: should be 2^5 = 32
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
