Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Extend the function to num_permutation (n, k=None) which takes in an additional optional keyword argument k, and returns the INTEGER number of k -permutations
Extend the function to num_permutation (n, k=None) which takes in an additional optional keyword argument k, and returns the INTEGER number of k -permutations of n items. The number is given by the formula Osk Unfortunately, there is not much improvement. Nevertheless, we can efficiently compute the number of k-permutations based on the previously computed number of k 1-permutations: For k from 0 ton, Pat-1 if k>0 Pn.k = nx (n 1) x ...x (n k + 1). (6) k terms in the product. Exercise Use the yield statement to write the function num_permutation_sequence(n) that returns a generator of Pnk with k from 0 to n. In [ ]: - def num_permutation_sequence (n): output = 1 for k in range (0, n + 1) : # YOUR CODE HERE executed in 3ms, finished 12:00:19 2020-10-25 In [ ]: # tests executed in 201ms, finished 12:00:19 2020-10-25 Exercise (Challenge) Extend the function num_permutation_sequence(n) so that calling send (0) method causes the generator to increment n instead of k for the next number to generate. .e., for 0 < k < n, send(0) ... Pnk-1 Pk P+1.k Pr+1,k+1 ... (7) where without labels is the normal transition without calling the send method. Hint: n+1 Pn+1,k = Pn.k X (8) n-k +1 In [ ]: def num_permutation_sequence (n): # YOUR CODE HERE raise NotImplementedError() executed in 14ms, finished 12:00:19 2020-10-25 In [ ]: # tests executed in 20ms, finished 12:00:19 2020-10-25 Exercise: Create a decorator to eliminate duplicate input positional arguments instead of the ouput, i.e., permutation (1,1,2) will return the same result as permutation(1,2). In [ ]: - def deduplicate_input (f): ''Takes in a function f that takes a variable number of arguments possibly with duplicates, returns a decorator that remove duplicates in the positional argument.''' @functools.wraps (f) def wrapper(*args, **kwargs): # YOUR CODE HERE raise NotImplementedError() return wrapper executed in 9ms, finished 12:00:19 2020-10-25 In [ 1: # tests executed in 12ms, finished 12:00:24 2020-10-25
Step by Step Solution
★★★★★
3.50 Rating (170 Votes )
There are 3 Steps involved in it
Step: 1
Solution is here def numper...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started