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']))- A. 1 3
- B. 0 2
- C. 0 3
- 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
- What is printed? python from functools import cache @cache def ways(s, i=0): if i == len(s): return 1 if s[i] == '0': return 0 total =…
- What is printed? python import heapq words = ['bb', 'a', 'cc', 'dd', 'e'] print(heapq.nlargest(2, words, key=len), heapq.nsmallest(2…
- What is printed? python data = [3, 8, 15, 4, 22] res = [y for x in data if (y := x 2) > 20] print(res, y)
- A sorted array holds 10^6 integers and receives 10^5 queries asking how many values are at most x. Which approach is fastest in CPython?
- What is printed? python def depth(n): return 0 if n == 0 else 1 + depth(n - 1) try: print(depth(500)) print(depth(5000)) except…
- What is printed? python from collections import defaultdict d = defaultdict(list) d['a'].append(1) print('b' in d, d['b'], len(d), 'b' in d)