ProgrammingHardTCS NQT · Wipro pattern
What is printed?
What is printed?
def tile(n):
dp = [0] * (n + 1)
dp[0] = 1
for i in range(1, n + 1):
dp[i] = dp[i - 1] + (dp[i - 2] if i >= 2 else 0)
return dp[n]
print([tile(k) for k in range(6)])- A. [0, 1, 1, 2, 3, 5]
- B. [1, 1, 2, 3, 5, 8]
- C. [1, 1, 2, 3, 5, 9]
- D. [1, 2, 3, 5, 8, 13]
Correct answer: B. [1, 1, 2, 3, 5, 8]
Each length is the sum of the two previous lengths, which is the Fibonacci recurrence. The base case dp[0] = 1 gives 1, 1, 2, 3, 5, 8 for lengths 0 to 5.
More Python Programming questions
- A program sorts the whole list again for each of 10^5 queries on a list of 10^5 items. Which statement about running it in CPython is…
- What is printed? python from collections import Counter def mw(s, t): need = Counter(t) have = {} formed = 0 req = len(need) l = 0 best =…
- What is printed? python import bisect board = [] for name, score in [('a', 5), ('b', 9), ('c', 5)]: bisect.insort(board, (-score, name))…
- What is printed? python print(sum([1, 2], 0.5))
- What is printed? python import heapq def topo(n, edges): indeg = [0] n adj = [[] for in range(n)] for u, v in edges: adj[u].append(v)…
- Which operation is O(1) amortised in CPython and removes the first item, while list.pop(0) is O(n)?