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...
-
Mr. JK, a U.S. citizen and resident of Vermont, owns 100 percent of the stock of JK Services, which is incorporated under Vermont law and conducts business in four counties in the state. JK Services...
-
Find the positive values of x and y that minimize S = x + y if xy = 36, and find this minimum value.
-
Reliable Machinery Inc. is considering expanding its operations in Thailand. The initial analysis of the project yields the following results: The project is expected to generate \($85\) million in...
-
1. List possible objects in the registration system, including their attributes and methods. 2. Identify possible use cases and actors. 3. Create a use case diagram that shows how students register....
-
E3.4 (LO 1, 2, 3), AP Luong Corporation encounters the following situations: 1. Luong collects $1,300 from a customer in 2022 for services to be performed in 2023. 2. Luong incurs utility expense...
-
Ace Companys income statements for the three years 2012, 2011, and 2010 are given below (all amounts in $ millions): 1. Other income (expense) comprised the following: 2. In 2012, cost of goods sold...
-
Some economists have argued that leagues expand strategically to prevent the formation of rival leagues, but expansion of any business enterprise occurs in response to new (profitable) opportunities....
-
The equation A(t)=4000005 gives the balance after t years of an initial investment of 4000 dollars which pays 5.00% compounded continuously. a. dA Find a formula for dt b. Find and interpret A'(8)....
-
What is the reaction that results to their pH? Following theexample of isopropyl alcohol. Kindly answer each one of them. Thankyou! For example pH value pH Paper Picture Chemical and Its Formula...
-
At the end of the current year, the accounts receivable account has a debit balance of $1,043,000 and sales for the year total $11,830,000. a. The allowance account before adjustment has a debit...
-
A farmer sells almonds according to the chart below. The more pounds of almonds that are sold, the lower the cost per pound is for the entire order of almonds. Number of Pounds up to 10 more than 10,...
-
Graph the function below and find a suitable table of values (you can graph the function and/or look at a table of values with your graphing calculator) to estimate the limit numerically or state...
-
Write a paper in which you consider why we would see a difference between POH calculated takeoff and landing performance data versus the actual takeoff and landing performance data. Show examples...
-
The following data are supplied for the common stocks of Nikola Corporation, Tesla, Inc. and General Motors: Nikola Corp (NKLA) Tesla Inc. (TSLA) Close Price ($) Close Price ($) 67.53 30.00 40.81...
-
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?
-
Explain at least three ways to reduce ATM fees charged to your account. Also describe the trade-offs involved in each action. Write About It Create a list with your explanations.
-
Bank X offers a checking account with a minimum balance of \($300\), but does not charge fees nor give interest. Bank Ys minimum balance is \($50\), but it charges a fee of \($25\) if you have more...
-
List the steps to take to use a checking account effectively.
Study smarter with the SolutionInn App