ProgrammingVery hardCAT · MBA placement

What is printed?

What is printed?
from functools import lru_cache
@lru_cache(maxsize=None)
def f(n, last):
    if n == 0:
        return 1
    total = f(n - 1, 0)
    if last == 0:
        total += f(n - 1, 1)
    return total
print(f(5, 0), f(1, 1))
  1. A. 13 1
  2. B. 8 1
  3. C. 13 2
  4. D. 21 1

Correct answer: A. 13 1

f counts binary strings of a given length with no two adjacent 1s, which follows the Fibonacci numbers, so length 5 gives 13. f(1, 1) is 1 because after a 1 the only choice is 0.

More Python Programming questions

Exams that ask this topic