I created this function for my homework for python 3.5 and my teacher emailed us
ID: 3733453 • Letter: I
Question
I created this function for my homework for python 3.5 and my teacher emailed us back and said that we could not use comprehension, but that we had to use a for loop instead. I am having trouble switching my function from comprehension to a for loop with keeping the function recursive def powerset (A) "Build the poverset 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 recurston flrst base case: size of ALSO second base case: size of A is 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 patlent, are not concerned about the due date, and have a lot of spare cash for memory upgrades f that is not clear, watch the Eames video agaLn) >>powerSet (11 21 11. 121. 1.21 Params: A (ist)s finite set of integers, represented internally as a Returnss (3Lst) powerset of A, represented internally as a 1ist l1st return 0 else bear " A-Pop ( element pulled fron list return powerset (ASD(bear]xfor x in powezset (A)Explanation / Answer
def powerset(A):
li=[[]];
for m in range(1,pow(2,len(A))):
i=m;j=0;k=[];
while(i>0):
if(i&1):
k.append(A[j]);
i=i>>1;
j=j+1;
li.append(k);
print(li);
powerset([1,2,3]);
# this is the iterative version of the powerset of a set and we can convert this into recursive one also with #base condition as limit < 2^n; I hope u'll be satisfied . For any additional query comment it...
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.