ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
from functools import lru_cache
def wb(s, words):
    @lru_cache(maxsize=None)
    def go(i):
        if i == len(s):
            return 1
        return sum(go(i + len(w)) for w in words if s.startswith(w, i))
    return go(0)
print(wb('catsandog', ['cat', 'cats', 'and', 'sand', 'dog']), wb('pineapplepenapple', ['apple', 'pen', 'applepen', 'pine', 'pineapple']))
  1. A. 1 3
  2. B. 0 2
  3. C. 0 3
  4. D. 0 4

Correct answer: C. 0 3

The memoised function counts the ways to split the string into dictionary words. catsandog has no full split, and pineapplepenapple has three.

More Python Programming questions

Exams that ask this topic