ProgrammingVery hardCognizant GenC · TCS NQT
What is printed?
What is printed?
def arrows(p):
p = sorted(p, key=lambda x: x[1])
cnt = 0
end = None
for s, e in p:
if end is None or s > end:
cnt += 1
end = e
return cnt
print(arrows([(10, 16), (2, 8), (1, 6), (7, 12)]), arrows([(1, 2), (3, 4), (5, 6), (7, 8)]))- A. 3 4
- B. 2 3
- C. 2 4
- D. 1 4
Correct answer: C. 2 4
Sorting by right end and shooting whenever a balloon starts after the last arrow's end is the correct greedy. The first list needs 2 arrows, and four disjoint balloons need 4.
More Python Programming questions
- What is printed? python from functools import cmptokey def largest(nums): s = [str(x) for x in nums] s.sort(key=cmptokey(lambda a, b: -1…
- What is printed? python from itertools import islice, count, takewhile sq = (i i for i in count(1)) print(list(islice(sq, 3))…
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def get(n): return [n] 2 a = get(3) a.append(99) b = get(3)…
- What is printed? python from collections import Counter a = Counter('aabbbc') b = Counter('abbd') print(a - b, (a + b)['b'], sorted((a &…
- What is printed? python print(pow(2, 100, 97), 2 100 % 97, (-2) 3 % 5, pow(-2, 3, 5))
- What is printed? python from itertools import permutations, combinations print(len(list(permutations('abcd', 2)))…