Question: This needs to be in Python code This lab will hopefully give you some idea of how set theory (in particular, the notion of subsets)
This needs to be in Python code
This lab will hopefully give you some idea of how set theory (in particular, the notion of subsets) can be applied towards Interesting Real-World ProblemsTM 1. (15 pts) Consider a set S of n elements, {ai, a2, ..., an}. As we YO DAWG,L HEARD YOULIKE SETS discussed in class, the power set of S, denoted P(S), is the set of all subsets of S. We know that there are 2" subsets of S, counting the empty set . Write a function powerSet(s) that returns the power set of s. You can represent the sets using Python lists. The empty set can be represented with the empty list, [l. The powerSet function should return a list of lists. For example, calling powerSet(1,2] should return something like [ [). [1], [2], [1,2]] This problem will likely take a little thought, but it's doable! Note that you can find all the subsets of A recursively. To understand the algorithm, first observe that given a set A and any element x e A, the subsets of A can be divided evenly into those that contain x and those that do not. For example, let's consider the element 2 from the set {1, 2,3) Subsets of {1,2,3} that contain 2: Subsets of 11,2, 3} that do not contain 2: (total: 4 subsets) (total: 4 subsets) We can approach the problem of finding all subsets of 1,2, 3 like this: First, remove 2 and find all subsets of (1,3): Now, make a copy of those subsets, and insert 2 into each of them: Combine the two sets of subsets from above to get the answer 12), 11,2}, 12,3), 11,2,3) Now we can generalize! To find all subsets of set A: Pick an arbitrary element E A, and remove from A Find all subsets of A (which is now smaller by one element).(this looks awfully recursive Now, make a copy of those subsets, and add x to each of them Combine the two sets of subsets from above to get the answer ) What should your base case bef? Python hint: Suppose you have a list named list1 and you want to make a copy of it. Writing list2 = list! is legal, but it only creates a shallow copy. Any changes to listl will also be reflected in list2. (This is the same idea as having two references to the same array in Java.) To make list2 independent from list1, you can ask Python to make a deep copy. An easy way of doing this is to use the built-in deepcopy function, located in the copy module import copy list2 = copy . deepcopy (list1)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
