Recursive Power and Count
Python
25 pts
Recursive Power and Count
Write two recursive functions (no loops and no ** operator — each function must call itself):
power(base, exponent)returnsbaseto the power ofexponent(a whole number 0 or more). Base case: any number to the power 0 is1. Recursive case:base * power(base, exponent - 1).count_items(items)returns how many items a list has. Base case: an empty list has 0 items. Recursive case:1 + count_items(items[1:]).
Print power(2, 10) and count_items(["Asmara", "Keren", "Massawa"]).
Expected output:
1024 3
Checklist
- Define the two functions
powerandcount_items. - Make
power(base, exponent)recursive, with the base case "any number to the power 0 is 1". - Make
count_items(items)recursive, with the base case "an empty list has 0 items". - Print
power(2, 10)and the count of the three cities.
🔑 Solution
If you view the solution before passing, you earn half points for this exercise. You still need to type it and press Check to finish.
How it works, step by step
Point at a step to highlight its lines. Type the solution yourself and press Check answer to finish.
