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))- A. 13 1
- B. 8 1
- C. 13 2
- 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
- What is printed? python a = [1, 2, 4] cnt = 0 for mask in range(1 << len(a)): s = sum(a[i] for i in range(len(a)) if mask >> i & 1) if s %…
- What is printed? python d = {'a': 1, 'b': 2, 'c': 3} d['a'] = 9 del d['b'] d['b'] = 5 print(list(d.items()))
- What is printed? python import bisect a = [(1, 'x'), (3, 'y'), (3, 'z'), (7, 'w')] i = bisect.bisectleft(a, 3, key=lambda p: p[0]) j =…
- Which statement about counting pairs i < j with a[i] + a[j] == t is FALSE?
- What is printed? python a = [] for i in range(20): a.append(i) print(len(a), a[-1], a.pop(), len(a))
- What is printed? python print(int('0012'), int('ff', 16), int('0b11', 0), int('1234'))