ProgrammingVery hardGATE CS & IT · Infosys
What is printed?
What is printed?
from functools import lru_cache
@lru_cache(maxsize=None)
def p(n, m):
if n == 0:
return 1
if n < 0 or m == 0:
return 0
return p(n - m, m) + p(n, m - 1)
print(p(10, 10), p(5, 5))- A. 42 6
- B. 41 7
- C. 43 7
- D. 42 7
Correct answer: D. 42 7
p(n, m) counts partitions of n into parts of size at most m. The number of partitions of 10 is 42 and of 5 is 7, which the cached recurrence reaches without repeating work.
More Python Programming questions
- What is printed? python import math print(sum(map(int, str(math.factorial(10)))))
- What is printed? python x = 'abc' print(x.islower() and x.isalpha(), ''.isdigit())
- What is printed? python it = iter('ab') print(next(it), list(it), next(it, 'end'))
- What is printed? python print([n for n in range(8) if n and n & (n - 1) == 0])
- What is printed? python import heapq a = [5, 1, 4] heapq.heapify(a) print([heapq.heappop(a) for in range(len(a))], a)
- What is printed? python vals = [y for x in range(5) if (y := x x) % 2 == 0] print(vals)