Question: Exercise: Implement a function combinations ( k , elements ) implementing the function C above, that is , returning the set of k - elements

Exercise: Implement a function combinations (k, elements) implementing the function C above, that is, returning the set of k-elements subsets of elements.
Hint: the difficulty is not in writing the recursion, but in deciding when NOT to recur, and rather, yield a result or the absence of results.
Exercise: Generating Combinations
1 #@title Exercise: Generating Combinations
2
def combinations(k, elements):
assert isinstance(elements, list)
# This can be done in 9 lines of code, and possibly fewer.
### YOUR SOLUTION HERE
(1)1 # Tests 5 points: Basic tests for Combination Generation
2
3 # Let us start from some base cases.
4L=[1,2,3,4,5]
5
6 # There are no combinations of 5 elements in groups of 6.
7n=0
8 for c in combinations (6,L) :
9,n+=1
10 check_equal (n,)
11
12
13 # There is only one combination of 5 elements in groups of 5 : the set itself.
14n=0
15 for c in combinations (5,L) :
16 check_equal (c,set(L))
17,n+=1
18 check_equal (n,1)
19
 Exercise: Implement a function combinations (k, elements) implementing the function C

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!