ProgrammingVery hardCAT · MBA placement
What is printed?
What is printed?
def pick_by_start(iv):
iv = sorted(iv)
cnt, end = 0, -10**9
for s, e in iv:
if s >= end:
cnt += 1
end = e
return cnt
print(pick_by_start([(1, 10), (2, 3), (4, 5)]))- A. 0
- B. 2
- C. 3
- D. 1
Correct answer: D. 1
Sorting by start takes (1, 10) first, which ends at 10 and blocks both short intervals. Sorting by finish time would take (2, 3) and (4, 5) and give 2.
More Python Programming questions
- In CPython, which operation on a collections.deque is O(n) in the worst case?
- What is printed? python def primes(n): s = [True] (n + 1) s[0:2] = [False, False] for i in range(2, int(n 0.5) + 1): if s[i]: s[i i::i] =…
- What is printed? python def merge(a, b): i = j = 0 out = [] while i < len(a) and j < len(b): if a[i] <= b[j]: out.append(a[i]) i += 1…
- What is printed? python from collections import deque def maxdist(g): R, C = len(g), len(g[0]) d = [[-1] C for in range(R)] q = deque()…
- What is printed? python from collections import Counter c = Counter('mississippi') print(c.mostcommon(2))
- What is printed? python def maxprod(a): best = curmax = curmin = a[0] for x in a[1:]: cands = (x, curmax x, curmin x) curmax, curmin =…