ProgrammingVery hardCodeforces-style · placement coding round
What is printed?
What is printed?
from functools import cache
@cache
def cat(n):
if n <= 1:
return 1
return sum(cat(i) * cat(n - 1 - i) for i in range(n))
print([cat(k) for k in range(6)], cat.cache_info().currsize)- A. [1, 1, 2, 5, 14, 42] 6
- B. [1, 1, 2, 5, 14, 42] 5
- C. [1, 1, 2, 5, 14, 41] 6
- D. [1, 2, 5, 14, 42, 132] 6
Correct answer: A. [1, 1, 2, 5, 14, 42] 6
The memoised recurrence builds the Catalan numbers, and each value from 0 to 5 gets exactly one cache entry. The sequence starts 1, 1, 2, 5, 14, 42.
More Python Programming questions
- What is printed? python from functools import lrucache grid = [[1, 3, 1], [1, 5, 1], [4, 2, 1]] @lrucache(maxsize=None) def best(r, c): if…
- What is printed? python def sigma(n): return sum(d for d in range(1, n + 1) if n % d == 0) perfect = [n for n in range(2, 30) if sigma(n)…
- What is printed? python def phi(n): res, p = n, 2 while p p <= n: if n % p == 0: while n % p == 0: n //= p res -= res // p p += 1 if n >…
- What is printed? python def cntinc(a, k): dp = [[0] (k + 1) for in range(len(a))] for i in range(len(a)): dp[i][1] = 1 for j in range(i)…
- What is printed? python from itertools import count, islice print(list(zip('abc', count(10, 5))), list(islice(count(1, 2), 3)))
- What is printed? python d = {k: v for k, v in [('a', 1), ('b', 2), ('a', 3)] if v > 1} print(d, {v: k for k, v in d.items()})