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'))
  1. A. 3 0 3
  2. B. 2 0 2
  3. C. 3 1 2
  4. 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

Exams that ask this topic