ProgrammingModerateTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import heapq
events = [(3, 'end'), (1, 'start'), (2, 'mid')]
heapq.heapify(events)
out = []
while events:
t, name = heapq.heappop(events)
out.append(f'{name}@{t}')
print(' '.join(out))- A. end@3 mid@2 start@1
- B. start@1 end@3 mid@2
- C. mid@2 start@1 end@3
- D. start@1 mid@2 end@3
Correct answer: D. start@1 mid@2 end@3
heappop returns the smallest tuple first, so events come out in order of time. The output is in ascending time order.
More Python Programming questions
- What is printed? python word = 'level' print(word == word[::-1], 'ab' 2 == 'abab', list('ab') == ['a', 'b'], ('a',) 0 == ())
- What is printed? python parent = list(range(5)) def find(x): while parent[x] != x: parent[x] = parent[parent[x]] x = parent[x] return x…
- What is printed? python trie = {} for w in ['car', 'cat', 'dog']: node = trie for ch in w: node = node.setdefault(ch, {}) node['#'] = True…
- What is printed? python h = 0 for ch in 'abc': h = (h 31 + ord(ch)) % 1000 print(h)
- What is printed? python grid = ['a..', '.a.', '...'] cnt = sum(row.count('.') for row in grid) diag = ''.join(grid[i][i] for i in…
- What is printed? python from collections import Counter c = Counter('abracadabra') print(sum(v for v in c.values() if v % 2), len(c)…