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))
  1. A. 42 6
  2. B. 41 7
  3. C. 43 7
  4. 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

Exams that ask this topic