ProgrammingModerateTCS NQT · Accenture
What is printed?
What is printed?
a = [4, 1, 7, 3, 2]
pre = [0]
for x in a:
pre.append(pre[-1] + x)
def rng(l, r):
return pre[r] - pre[l - 1]
print(rng(2, 4), rng(1, 5), rng(5, 5))- A. 10 17 2
- B. 11 16 2
- C. 11 17 3
- D. 11 17 2
Correct answer: D. 11 17 2
pre[i] holds the sum of the first i items, so rng(l, r) = pre[r] - pre[l-1]. Here rng(2,4) = 1+7+3 = 11, rng(1,5) = 17 and rng(5,5) = 2.
More Python Programming questions
- What is printed? python def mostwater(h): i, j, best = 0, len(h) - 1, 0 while i < j: best = max(best, min(h[i], h[j]) (j - i)) if h[i] <…
- What is printed? python def longest(s): last = {} l = best = 0 for r, ch in enumerate(s): if ch in last and last[ch] >= l: l = last[ch] +…
- What is printed? python import bisect a = sorted([5, 1, 9, 5, 3, 7, 5]) lo = bisect.bisectleft(a, 3) hi = bisect.bisectright(a, 7)…
- A Python program must answer 10^6 membership queries (x in container) against a container with 10^6 items. Which statement is correct?
- What is printed? python def ways(coins, amt): dp = [0] (amt + 1) dp[0] = 1 for c in coins: for v in range(c, amt + 1): dp[v] += dp[v - c]…
- What is printed? python def lcs(a, b): dp = [[0] (len(b) + 1) for in range(len(a) + 1)] for i in range(1, len(a) + 1): for j in range(1…