ProgrammingHardCognizant GenC · TCS NQT
What is printed?
What is printed?
from functools import cache
@cache
def ways(s, i=0):
if i == len(s):
return 1
if s[i] == '0':
return 0
total = ways(s, i + 1)
if i + 1 < len(s) and 10 <= int(s[i:i+2]) <= 26:
total += ways(s, i + 2)
return total
print(ways('226'), ways('06'), ways('11106'))- A. 3 0 3
- B. 2 0 2
- C. 3 1 2
- D. 3 0 2
Correct answer: D. 3 0 2
Each position decodes one digit or two digits forming 10 to 26. '226' has 3 decodings, '06' has none because a leading zero is invalid, and '11106' has 2.
More Python Programming questions
- 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)
- What is printed? python m = [[1, 2, 3], [4, 5, 6]] rot = [list(r) for r in zip(m[::-1])] print(rot)