ProgrammingHardCognizant GenC · TCS NQT
What is printed?
What is printed?
import heapq
h = []
for w, name in [(2, 'b'), (1, 'z'), (2, 'a'), (1, 'c')]:
heapq.heappush(h, (w, name))
out = []
while h:
out.append(heapq.heappop(h)[1])
print(''.join(out))- A. zcab
- B. czba
- C. czab
- D. zcba
Correct answer: C. czab
Tuples compare element by element, so equal weights are ordered by name. That gives (1,'c') before (1,'z') and (2,'a') before (2,'b').
More Python Programming questions
- What is printed? python recs = [('a', 2), ('b', 1), ('c', 2), ('d', 1)] print([n for n, v in sorted(recs, key=lambda r: r[1]…
- What is printed? python pairs = list(zip([1, 2, 3], 'ab', [True])) print(pairs, len(list(zip())))
- What is printed? python def threeless(a, t): a = sorted(a) n = len(a) c = 0 for i in range(n - 2): l, r = i + 1, n - 1 while l < r: if…
- What is printed? python import heapq def rooms(iv): iv = sorted(iv) h = [] for s, e in iv: if h and h[0] <= s: heapq.heapreplace(h, e)…
- For a Python dict d with n entries, which operation costs O(n) rather than O(1) on average?
- With n = 2 x 10^5 and a 1-second limit in CPython, which algorithm can be rejected because it is too slow?