ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
def hanoi(n):
    return 0 if n == 0 else 2 * hanoi(n - 1) + 1
def calls(n):
    return 1 if n == 0 else 1 + 2 * calls(n - 1)
print(hanoi(5), calls(3))
  1. A. 32 15
  2. B. 31 15
  3. C. 31 8
  4. D. 16 15

Correct answer: B. 31 15

hanoi follows h(n) = 2h(n-1) + 1 with h(0) = 0, so h(n) = 2^n - 1 = 31. calls follows c(n) = 1 + 2c(n-1) with c(0) = 1, so c(n) = 2^(n+1) - 1 = 15.

More Python Programming questions

Exams that ask this topic