What changes when you ask one person and wait instead of doing all the bookkeeping yourself — and how does that recipe write power_recur almost line for line?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
Here is the finished function from the video:
def power_recur(n, p): if p == 0: return 1 elif p == 1: return n else: return n * power_recur(n, p - 1)
Trace power_recur(3, 5) on paper. Counting the very first call itself, how many separate calls to power_recur are made in total before the final answer comes back? Assign that whole number to num_calls.
The regrade chain, looped versus passed along
Describe the student visiting instructor, TA, lab assistant and grader while totalling the scores, then the single request handed down the chain to the grader with the total assembled coming back up.
Algorithmic and semantic definitions of recursion
Write both: algorithmically, reduce a problem to the same problem slightly changed until a base case starts the conquer step; semantically, a function that calls itself with changed parameters.
power_recur written from the mathematical definition
Write the definition of n to the power p first, then the code with p == 0 returning 1, p == 1 returning n, else n*power_recur(n, p-1), and record that power_recur(2,3) gives 8.
More than one base case
Record that a recursive function may carry several base cases and that p == 0 here is reached only when the caller passes 0 directly.