ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def solve(n, memo={}):
if n in memo:
return memo[n]
if n < 2:
return n
memo[n] = solve(n - 1) + solve(n - 2)
return memo[n]
print(solve(30), len(solve.__defaults__[0]))- A. 832040 30
- B. 514229 28
- C. 832040 31
- D. 832040 29
Correct answer: D. 832040 29
solve(30) is the 30th Fibonacci number, 832040. The default memo dict stores keys 2 through 30, which is 29 entries, because n < 2 returns before any storage.
More Python Programming questions
- What is printed? python x = 7 match x: case 1 | 2: r = 'small' case int(v) if v > 5: r = f'big{v}' case : r = 'other' print(r)
- What is printed? python a = [[1, 2], [3]] b = a[:] b[1].append(4) b.append([5]) print(a, b, len(a))
- What is printed? python print(bool([]), bool([0]), bool(0.0), bool('False'), bool(None) or 'x')
- What is printed? python import heapq events = [(3, 'end'), (1, 'start'), (2, 'mid')] heapq.heapify(events) out = [] while events: t, name…
- What is printed? python word = 'level' print(word == word[::-1], 'ab' 2 == 'abab', list('ab') == ['a', 'b'], ('a',) 0 == ())
- What is printed? python parent = list(range(5)) def find(x): while parent[x] != x: parent[x] = parent[parent[x]] x = parent[x] return x…