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...
-
1. Do managers like Janis Blancero face a more complicated decision when evaluating the personal requests of employees versus evaluating employees' individual work performance? Explain. 2. (a) Should...
-
Presented below is the partial bond discount amortization schedule for Osaki Corp., which uses the effective-interest method of amortization. Instructions (a) Prepare the journal entry to record the...
-
What does the variance tell us about data points?
-
XYZ is a calendar-year corporation that began business on January 1, 2015. For 2015, it reported the following information in its current year audited income statement. Notes with important tax...
-
Golden Paper Company purchases a 30-day commercial paper with a maturity value of $500,000 at a market price of $498,000. Calculate the bond equivalent yield and the effective annual percentage rate...
-
Donovan Ramsey, the Chief Financial Officer of LevelUp Business Consulting, has advised you that the company is considering closing its Calgary, Alberta office and transitioning the staff of that...
-
Initial Post: Fraud is the intentional act to misappropriate (steal) assets or misstate the financial statements. Discuss one company that has recently been charged/accused of an accounting scandal....
-
Your new podcast channel, School2Biz, offers advice to business students making the transition from college to career. You provide information on everything from preparing rsums to interviewing to...
-
Considering what youve learned about nonverbal communication, what are some of the ways in which communication might break down during an online meeting in which the participants can see video images...
-
Your web design start-up has been commissioned to create a distinct profile page for a client. You have told your team to visit the profile pages of three of the clients competitors. Analyze the...
-
You and your tutoring team (see the previous case) know you should take advantage of every social media platform to inform potential clients about your new company. Your task With the same team as in...
-
As a team or department leader, what steps can you take to ensure that your meetings are successful and efficient?
-
Nina can ride her bike 63, 360 feet in 3, 400 seconds, and Sophia can ride her bike 10 miles in 1 hour. What is Nina's rate in miles per hour f there are 5, 280 feet in a mile?
-
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?
-
Do the assumptions for Bernoulli trials appear to hold? Explain. If the assumptions hold, identify success and the probability of interest. (a) A TV ratings company will use their electronic...
-
Use Table 1, or software, to find (a) \(B(7 ; 18,0.45)\); (b) \(b(7 ; 18,0.45)\); (c) \(B(8 ; 11,0.95)\); (d) \(b(8 ; 11,0.95)\); (e) \(\sum_{k=4}^{11} b(k ; 11,0.35)\); (f) \(\sum_{k=2}^{4} b(k ;...
-
Which conditions for the binomial distribution, if any, fail to hold in the following situations? (a) The number of persons having a cold at a family reunion attended by 30 persons. (b) Among 8...
Study smarter with the SolutionInn App