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))- A. 32 15
- B. 31 15
- C. 31 8
- 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
- What is printed? python import sys def depth(n): try: return depth(n + 1) except RecursionError: return n d = depth(0) print(d > 500, d <…
- What is printed? python x = 0 def a(): global x x = 1 def b(): global x x += 10 b() return x print(a(), x)
- What is printed? python class A: n = [] m = 0 a, b = A(), A() a.n.append(1) a.m += 1 b.n = [2] print(A.n, A.m, a.n, b.n, a.m, b.m)
- What is printed? python x = [0, 1, 2, 3, 4, 5] print(x[1::2], x[-1::-2], x[::-1][::2] == x[::-2])
- What is printed? python m = [[1, 2, 3], [4, 5, 6]] t = list(zip(m)) print(t, [list(r) for r in zip(t)] == m, [x for row in m for x in…
- What is printed? python grid = [[0] 3 for in range(3)] for i in range(3): grid[i][i] = grid[i - 1][i - 1] + 1 print(grid[0][0]…