ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import heapq
def jobs(items):
items = sorted(items, key=lambda x: x[0])
h = []
for d, p in items:
heapq.heappush(h, p)
if len(h) > d:
heapq.heappop(h)
return sum(h)
print(jobs([(2, 100), (1, 19), (2, 27), (1, 25), (3, 15)]), jobs([(1, 5), (1, 9)]))- A. 127 9
- B. 142 9
- C. 142 14
- D. 100 9
Correct answer: B. 142 9
Process jobs by deadline and keep a min-heap of profits that the deadlines allow. Dropping the smallest profit whenever the heap overflows gives 142 for the first list and 9 for the second.
More Python Programming questions
- What is printed? python try: print(sorted([3, None, 1])) except TypeError: print('TypeError') print(max(['b', 'a'], key=str.upper)…
- What is printed? python s = 'hello' try: s[0] = 'j' except TypeError: print('immutable') t = s.replace('h', 'j') print(t, s)
- What is printed? python print(1020 / 1019, 1020 // 1019, 264 % 1000, 1020 == 1e20)
- What is printed? python calls = [] def f(x): calls.append(x) return x 2 m = map(f, [1, 2, 3]) print(calls) first = next(m) print(calls…
- In Python, when is the outermost iterable of a generator expression such as (x for x in make()) evaluated?
- What is printed? python class Countdown: def init(self, n): self.n = n def iter(self): while self.n > 0: yield self.n self.n -= 2 c =…