Question

Python code The power set of a list L is a list containing all possible combinations of the eleme...

python code

The power set of a list L is a list containing all possible combinations of the elements of L. For example, the power set of [1, 2, 3] is [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]. Section 9.3 of your textbook describes a (subtle) algorithm for generating the power set.

• Read and understand the structure of the code in Figure 9.6

• Write (your own) function powerset(L) that generates and returns the power set of a list;

• What is the complexity of your algorithm?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def powerset(L):
    # set_size of power set of a set
    pow_set_size = (int)(math.pow(2, len(L)))
    counter = 0;
    j = 0;
    print("[]")
   
    for counter in range(1, pow_set_size):
        print("[",end=" ")
        for j in range(0, len(L)):

            # Check if jth bit in the
            # counter is set If set then
            # print jth element from set
            if ((counter & (1 << j)) > 0):
                print(L[j], end=" ")
        print("]")

    # Driver program to test printPowerSet


L = [1, 2, 3]
powerset(L)

Complexity is O(n2).

/* PLEASE UPVOTE THANK YOU IN ADVANCE. IF YOU SATISFY WITH THE ANSWER IF ANY QUERY ASK ME IN COMMENT SECTION */

Add a comment
Know the answer?
Add Answer to:
Python code The power set of a list L is a list containing all possible combinations of the eleme...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT