1. Write a program that contains the recursive functions below (please note for all the functions,...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
1. Write a program that contains the recursive functions below (please note for all the functions, you must use recursion and not loops or built in Python functions): a. def power(x, y): This function should recursively compute the power of a number (x represents the number and y represents the power to which its being raised - assume y will always be a positive integer) e.g., # 2³ = 8 #-2³ = -8 # 15 = 1 i. print(power(2, 3)) ii. print(power(-2, 3)) iii. print(power(1,5)) b. def cat_cars(n): If every cat has 2 ears, this function should recursively compute the total number of ears based off the number of cats (n represents the total number of cats) e.g., i. print(cat_ears(0)) #0-0 cats have 0 ears in total ii. print(cat_ears(1)) #2-1 cat has 2 ears in total iii.print(cat_cars (2)) #4-2 cats have 4 ears in total c. def alien_ears(n): We have aliens standing in a line, numbered 1, 2, ... The odd aliens (1, 3, ...) have 3 ears. The even aliens (2, 4, ...) have 2 ears. This function should return the total number of alien ears (n represents the total number of aliens) e.g., i. print(alien_ears(1)) ii.print(alien_ears(2)) #3 - (alien 1 has 3 cars) #5 - (alien 1 has 3 ears, alien 2 has 2 ears) 1. Write a program that contains the recursive functions below (please note for all the functions, you must use recursion and not loops or built in Python functions): a. def power(x, y): This function should recursively compute the power of a number (x represents the number and y represents the power to which its being raised - assume y will always be a positive integer) e.g., # 2³ = 8 #-2³ = -8 # 15 = 1 i. print(power(2, 3)) ii. print(power(-2, 3)) iii. print(power(1,5)) b. def cat_cars(n): If every cat has 2 ears, this function should recursively compute the total number of ears based off the number of cats (n represents the total number of cats) e.g., i. print(cat_ears(0)) #0-0 cats have 0 ears in total ii. print(cat_ears(1)) #2-1 cat has 2 ears in total iii.print(cat_cars (2)) #4-2 cats have 4 ears in total c. def alien_ears(n): We have aliens standing in a line, numbered 1, 2, ... The odd aliens (1, 3, ...) have 3 ears. The even aliens (2, 4, ...) have 2 ears. This function should return the total number of alien ears (n represents the total number of aliens) e.g., i. print(alien_ears(1)) ii.print(alien_ears(2)) #3 - (alien 1 has 3 cars) #5 - (alien 1 has 3 ears, alien 2 has 2 ears)
Expert Answer:
Answer rating: 100% (QA)
Below is a stepbystep explanation of the recursive functions power cat ears and alien ears implemented in Python 1 Power Function powerxy This function is designed to compute the power of a number x r... View the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
What kind of firms are being created in this age of technology? What are the differences between the two firms. What can bondholders do to prevent the implementation of Project Chariot?
-
Formulate a single python program that contains the following functions. Function 1: A function called frame_this which takes a list of words as a parameter (list_of_words) and then prints those...
-
Answer Problem 6.12 for a 90% CI. Refer to the data in Table 2.13. Regard this hospital as typical of Pennsylvania hospitals. Table 2.13: Hospital-stay data
-
If the velocity does not increase downstream, as stated in the previous question, what will have to change to accommodate the greater volume of water (i.e., discharge)?
-
In a paragraph or two, explain what a botnet is and how it could cause more damage than a single Trojan horse attack.
-
For the circuit of Figure P26.60, a. What is the resonance frequency? b. At resonance, what is the peak current through the circuit? FIGURE P26.60 (10 V) cos (2ft) i eeee-ww 1002 1.0 mH 1.0 F
-
Using the data given for Cookware, Inc., in E8A and assuming that the company uses the average costing method, determine the cost per equivalent set for August. Assume equivalent sets are 15,550 for...
-
eferences Knowledge Check 01 On January 1, Year 1, Greenfield, Incorporated issues $100,000 of 9% bonds maturing in 10 years when the market rate of interest is 8% Interest is paid semiannually on...
-
Using dimensional equations, convert (a) 2 wk to microseconds. (b) 38.1 ft/s to kilometers/h. (c) 554 m 4 / (day kg) to ft 4 / (min lbm).
-
for each reaction, label the lewis acid and base. Draw the product of the reaction. Use curved arrow notation to show the movement of electron pairs.
-
Cary and Bill form the equal CB Partnership. Cary contributes cash of $50,000 and Bill contributes land with a fair market value of $50,000 and adjusted basis of $35,000. How much gain or loss does...
-
If you are floating in the hypersaline Dead Sea, where the density of the salty water 1240 kg/m3, how much of your body is underwater? BTW your mass is 50 kg.
-
The inflation rate in the US is 5% while the inflation rate in Japan is 10%. The current exchange rate for the Japanese yen () is $0.0080. After supply and demand for the Japanese yen have adjusted...
-
Depreciation 4. Keith began a calendar-year car cleaning business called Cozart's Cars Palace. He bought $100,000 of car cleaning equipment and placed it in service in 2021. The asset has a 7-year...
-
2. CEO of Company X has 30 million shares of stocks and 30 million shares of options (of Company X). The stock return volatility (o) is 0.50, dividend yield (d) is 0.0, riskfree rate (r) is 0.05, and...
-
Example 3.4 Recall the Verizon case study in Section 1.3. Whether Verizon is judged to be making repairs slower for competitors' customers is determined using hypothesis tests, as mandated by the New...
-
Use the information given about the angles and to find the exact value of: (a) sin( + ) (b) cos( + ) (c) sin( - ) (d) tan ( + ) (e) sin(2) (f) cos (2) (g) sin /2 (h) cos/2 cos = 4/5, 0 < < /2; cos =...
-
Russell (age 50) and Linda (age 45) Long have brought you the following information regarding their income and expenses for the current year. Russell owns and operates a landscaping business called...
-
Matthew borrows $250,000 to invest in bonds. During 2012, his interest on the loan is $30,000. Matthew's interest income from the bonds is $10,000. This is Matthew's only investment income. a....
-
Jan has two jobs during 2012. One employer withheld and paid FICA taxes on $66,600 of Jan's salary, and the other employer withheld and paid FICA taxes on $44,400 in salary paid to Jan. Calculate the...
-
For coordinates \(\left(x^{1}, x^{2} ight)\) and metric \(g=\operatorname{diag}\left(g_{11}, g_{22} ight)\), the Gaussian curvature is For a sphere with coordinates defined in the following figure,...
-
Consider the holonomic basis defined in Box 26.1 . Using that the tangent vector for a curve can be written \(t=t^{\mu} e_{\mu}=\left(d x^{\mu} / d \lambda ight) e_{\mu}\), show that Thus, \(g_{\mu...
-
The Lie bracket of vector fields \(A\) and \(B\) is defined as their commutator, \([A, B]=\) \(A B-B A\). The Lie bracket of two basis vectors vanishes for a coordinate basis but not for a...
Study smarter with the SolutionInn App