1) Complete the code for the calcPower recursive function shown below, which is intended to raise the base number passed into the function to the exponent power passed into the function:
1. def calcPower(base, exponent): 2. answer = 0 3. if exponent == 0: 4. answer = 1 5. else: 6. _____________________________ 7. return answer
| answer = base * calcPower(base - 1, exponent) |
| answer = base * calcPower(base, exponent - 1) |
| answer = base * calcPower(base, exponent) |
| answer = base * calcPower(base - 1, exponent - 1) |
2) Consider the following recursive code snippet:
1. def mystery(n, m) : 2. if n == 0 : 3. return 0 4. if n == 1: 5. return m 6. return m + mystery(n - 1, m)
What value is returned from a call to mystery(3,6)
3) Consider the following recursive code snippet:
1. def mystery(n, m): 2. if n <= 0 : 3. return 0 4. if n == 1 : 5. return m 6. return m + mystery(n - 1, m)
Identify the terminating condition(s) of function mystery?
4) Consider the following code snippet for recursive addition:
1. def add(i, j) : 2. # assumes i >= 0 3. if i == 0 : 4. return j 5. else : 6. return add(i - 1, j + 1)
Identify the terminating condition in this recursive function.
| There is no terminating condition |
5) What is required to make a recursive function successful?
I. One or more special cases that handle the simplest computations directly II. A recursive call to a smaller or simpler version of the problem III. Mutually recursive calls