Question: PYTHON: 2. (6 points) The previous problem found all subsets of a given set S. Sometimes we want only the k-subsets of S that is,

PYTHON:

PYTHON: 2. (6 points) The previous problem found all subsets of a

2. (6 points) The previous problem found all subsets of a given set S. Sometimes we want only the k-subsets of S that is, the subsets with cardinality k. For example, consider a pizza joint that offers 16 different toppings. Call this set of 16 toppings T. The power set P(T) represents all possible combinations of toppings; there are 216 = 65,536 combinations. A customer who wants a 3-topping pizza would be interested in only the 3-subsets of T; it turns out that there are only 560 such subsets. (Later in the course, we'll discuss how to compute the count of k-subsets without actually finding the subsets!) So how do we find all the k-subsets of a set S? One obvious way is to start by finding P(S), which gives us all possible subsets. Then we can just iterate through all the subsets and include only those with a cardinality of k. Write a Python function k_subsets naive (S, k) that calls your previously written power_set function to return a set containing all the k-subsets of S. Again, represent the sets using Python lists. For example, calling k_subsets naive ([4, 5, 6], 2) should return something like [[4, 5], [4, 6], [5, 6]]. If k is not valid (i.e., negative, or greater than (S1), this function should return the empty list, []

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!