ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
def climb(n, steps):
dp = [0] * (n + 1)
dp[0] = 1
for i in range(1, n + 1):
for s in steps:
if i - s >= 0:
dp[i] += dp[i - s]
return dp[n]
print(climb(5, [1, 3, 4]), climb(4, [2, 3]))- A. 5 1
- B. 6 2
- C. 6 1
- D. 4 1
Correct answer: C. 6 1
dp[i] adds up the ways for each allowed step, counting different orders as different. Steps {1, 3, 4} give 6 ways for n = 5, and steps {2, 3} give exactly one way for n = 4, namely 2 + 2.
More Python Programming questions
- What is printed? python from collections import deque def rot(g): R, C = len(g), len(g[0]) q = deque() fresh = 0 for r in range(R): for c…
- What is printed? python import heapq a, b, c = [1, 4, 7], [2, 5], [0, 3, 6] print(list(heapq.merge(a, b, c))[:5], next(heapq.merge(b, a)))
- What is printed? python import heapq def kth(m, k): h = [(m[0][0], 0, 0)] seen = {(0, 0)} for in range(k - 1): v, r, c = heapq.heappop(h)…
- What is printed? python from itertools import groupby rows = [('b', 2), ('a', 1), ('b', 5), ('a', 3)] ok = {k: sum(v for , v in g) for k…
- What is printed? python def lru(cap, ops): d = {} out = [] for op, k in ops: if op == 'get': if k in d: v = d.pop(k) d[k] = v…
- What is printed? python from itertools import combinations a = [1, 2, 3, 4] cnt = 0 for r in range(1, len(a) + 1): for c in…