ProgrammingModerateTCS NQT · Accenture
What is printed?
What is printed?
import bisect a = sorted([5, 1, 9, 5, 3, 7, 5]) lo = bisect.bisect_left(a, 3) hi = bisect.bisect_right(a, 7) print(a, hi - lo)
- A. [1, 3, 5, 5, 5, 7, 9] 4
- B. [1, 3, 5, 5, 5, 7, 9] 6
- C. [1, 3, 5, 5, 5, 7, 9] 5
- D. [1, 3, 5, 5, 5, 7, 9] 3
Correct answer: C. [1, 3, 5, 5, 5, 7, 9] 5
bisect_left(3) is 1 and bisect_right(7) is 6, so the difference 5 counts the values 3, 5, 5, 5 and 7 that lie in the range [3, 7].
More Python Programming questions
- 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…
- What is printed? python items = [(60, 10), (100, 20), (120, 30)] cap = 50 best = 0 w = 0 for v, wt in sorted(items, key=lambda x: x[0] /…
- What is printed? python def mincapacity(w, days): lo, hi = max(w), sum(w) while lo < hi: mid = (lo + hi) // 2 need, cur = 1, 0 for x in w…
- What is printed? python from functools import lrucache @lrucache(maxsize=2) def sq(x): return x x for v in [1, 2, 1, 3, 1]: sq(v)…