ProgrammingHardInfosys HackWithInfy · TCS CodeVita
What is printed?
What is printed?
def make():
memo = {}
def fib(n):
if n in memo:
return memo[n]
memo[n] = n if n < 2 else fib(n - 1) + fib(n - 2)
return memo[n]
return fib
f = make()
print(f(50), len(f.__closure__))- A. 12586269025 1
- B. 12586269025 2
- C. 12586269025 3
- D. 12586268025 2
Correct answer: B. 12586269025 2
fib refers to memo and to itself, so its closure holds two cells. fib(50) is 12586269025, and the memo keeps the recursion linear.
More Python Programming questions
- What is printed? python words = 'the cat and the dog and the end'.split() cnt = {} for w in words: cnt[w] = cnt.get(w, 0) + 1 top =…
- What is printed? python def merge(iv): iv.sort() out = [iv[0]] for s, e in iv[1:]: if s <= out[-1][1]: out[-1][1] = max(out[-1][1], e)…
- What is printed? python import heapq def dijk(g, s): d = {s: 0} h = [(0, s)] while h: du, u = heapq.heappop(h) if du > d[u]: continue for…
- What is printed? python from collections import deque def topo(n, edges): indeg = [0] n adj = [[] for in range(n)] for u, v in edges…
- What is printed? python dp = [0] 16 for i in range(1, 16): dp[i] = dp[i >> 1] + (i & 1) print(dp[13], sum(dp))
- What is printed? python print([format(i ^ (i >> 1), '02b') for i in range(4)])