ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
from functools import lru_cache
grid = [[1, 3, 1], [1, 5, 1], [4, 2, 1]]
@lru_cache(maxsize=None)
def best(r, c):
if r == 0 and c == 0:
return grid[0][0]
opts = []
if r > 0:
opts.append(best(r - 1, c))
if c > 0:
opts.append(best(r, c - 1))
return grid[r][c] + min(opts)
print(best(2, 2), best.cache_info().misses)- A. 7 8
- B. 7 9
- C. 9 9
- D. 7 7
Correct answer: B. 7 9
The cheapest path to (2, 2) costs 7, and the cache misses once for each of the nine cells, since each cell is computed once.
More Python Programming questions
- 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()})
- What is printed? python first, mid, last = [1, 2, 3, 4, 5] a, (b, c) = 1, (2, 3, 4) print(mid, last, b, c)