Extend the function to num_permutation (n, k=None) which takes in an additional optional keyword argument...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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 <n Pn. Pak = { (5) otherwise. In [26]: def num_permutation(n,k=None): # YOUR CODE HERE executed in 4ms, finished 19:44:08 2020-11-02 In [27]: # tests assert isinstance(num_permutation (0), int) assert num_permutation(3) == 6 assert num_permutation(3,0) == 1 assert num_permutation(3,2) == 6 assert num_permutation(10,5) == 30240 executed in 20ms, finished 19:44:09 2020-11-02 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 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 <n Pn. Pak = { (5) otherwise. In [26]: def num_permutation(n,k=None): # YOUR CODE HERE executed in 4ms, finished 19:44:08 2020-11-02 In [27]: # tests assert isinstance(num_permutation (0), int) assert num_permutation(3) == 6 assert num_permutation(3,0) == 1 assert num_permutation(3,2) == 6 assert num_permutation(10,5) == 30240 executed in 20ms, finished 19:44:09 2020-11-02 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
Expert Answer:
Related Book For
Posted Date:
Students also viewed these mathematics questions
-
The following are the financial statements of Darussunnah Bhd: Darussunnah Bhd Statement of Profit or Loss and Other Comprehensive Income for the year ended 31 December 2017 RM Turnover 436,300,000...
-
Create and analyse an Engineering Economics project of your choice from the discussed applications in Chapter 1 in your lecture notes and text book. You must identify a project to study, collect the...
-
Please help me to discuss the results of the multitasking test from Addressing the Multitasking Myth. Describe effects of switchtasking that have personally or professionally experienced. What did...
-
Calculate the weighted average cost of capital for Genedak-Hogan for before and after international diversification. Did the reduction in debt costs reduce the firm's weighted average cost of...
-
A reaction of the form aA Products is second order with a rate constant of 0.169 L/(mols). If the initial concentration of A is 0.159 mol/L, how many seconds would it take for the concentration of A...
-
Standing waves (see Section 16. 6) can form when sound traveling inside a closed tube reflects from each end. These waves have a node at each (immovable) end. Standing waves can also form in a tube...
-
A model hydrofoil is to be tested. Is it practical to satisfy both the Reynolds number and the Froude number for the hydrofoil when it is operating near the water surface? Support your decision.
-
Victoria Company has the following account balances on December 31, 2016, prior to any adjustments: Additional adjustment information: (a) depreciation on buildings, $1,100; on equipment, $600; (b)...
-
What is community psychology (CP)?
-
On January 1, 2020, K. Crew Inc. reported a $6,000 credit balance in its Accumulated OCIPension Gain/Loss account related to its pension plan. During 2020, the following events occurred. Actual...
-
Spelling Basketball Company manufactures basketballs that retail for $ 15 each. The capacity of the current company allows them to only manufacture 1,000,000 basketballs every year, and they are able...
-
Based on Leenids analysis of the results of the initial screening, relative to Colorful Concepts the bond rating of Silk Road should be: A. lower. B. higher. C. the same. Sergei Leenid, CFA, is a...
-
Compared with using the Singapore dollar as Acceletrons functional currency for 2007, if the US dollar were the functional currency, it is most likely that Redlines consolidated: A. inventories will...
-
Confabulateds reported interest income would be lower if the cost was the same but the par value (in thousands) of: A. Bugle was 28,000. B. Cathay was 37,000. C. Dumas was 55,000. Burton Howard,...
-
When translating Acceletrons financial statements into US dollars, Redline is least likely to use an exchange rate of USD per SGD: A. 0.671. B. 0.588. C. 0.654. Redline Products, Inc. is a US-based...
-
Compared to Confabulateds reported earnings before taxes in 2009, if Bugle had been classified as a held for trading security, the earnings before taxes (in thousands) would have been: A. the same....
-
2. Explain how you derive the intertemporal budget constraint: C2 Ako a+ 3-k+ 1+r 1+r Given this budget constraint, characterize the optimal capital investment k of the country. Explain why it is...
-
Give the structural formulas of the alkenes that, on ozonolysis, give: a. (CH3)2C=O and CH2=O b. Only (CH3CH2)2C=O c. CH3CH=O and CH3CH2CH=O d. O=CHCH2CH2CH2CH=O
-
Paul Joubert retired from his firm. He has continued to hold his private retirement investments in a portfolio of common stocks and bonds. At the beginning of 2002, when he retired, his account was...
-
Distinguish between economic significance and statistical significance.
-
How do auditors and management relate to each other in determining the financial statements?
-
Basic Chemicals Ltd produces a highly flammable chemical product. The company experienced a flood on 1 April 2025 that destroyed its entire work in process inventory but did not affect the raw...
-
Cold Appliances Pty Ltd makes refrigerators and is trying to determine the cost of its ending work in process. The accountant has put together the following data for the year ended 30 June 2025. Each...
-
An apparatus for measuring thermal conductivity employs an electrical heater sandwiched between two identical samples of diameter \(25 \mathrm{~mm}\) and length \(60 \mathrm{~mm}\), which are pressed...
Study smarter with the SolutionInn App