def powerset (A) \"\"Build the powerset of A Given a set A, the powerset of A is
ID: 3733092 • Letter: D
Question
def powerset (A) ""Build the powerset of A Given a set A, the powerset of A is the set of all subsets of A. Hint: recursion, anyone? no, really, you need to use recursion see lecture for the way to think about powerset recursively Hint: there are two base cases to the recursion: first base case: size of A is 0 second base case: size of A is 1 Hint: when you are changing a list, cloning makes it safer Hint: you may not want to test this function on a set of size 40 unless you are very patient, are not concerned about the due date, and have a lot of spare cash for memory upgrades if that is not clear, watch the Eames video again) >>> powerSet (1,21 Params: A (list): finite set of integers, represented internally as a Returns: (1ist) powerset of A, represented internally as a list if len (A)0 or len (A)1 else: , 121, 11,21 1ist return t111 bear-. A.pop () # element pulled from list return powerset (A:)+[(bear] +x tor x in powerset (A)Explanation / Answer
def powerset(A): if len(A) == 0 or len(A) == 1: return [[]] else: bear = A.pop() lst = [] p = powerset(A) for x in p: lst.append(x) return powerset(A[:]) + [[bear] + lst]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.