ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
recs = [('a', 2), ('b', 1), ('c', 2), ('d', 1)]
print([n for n, v in sorted(recs, key=lambda r: r[1], reverse=True)])- A. ['c', 'a', 'd', 'b']
- B. ['a', 'c', 'd', 'b']
- C. ['d', 'b', 'c', 'a']
- D. ['a', 'c', 'b', 'd']
Correct answer: D. ['a', 'c', 'b', 'd']
reverse=True still keeps equal keys in their original order, because sorting is stable. The weight-2 records come first as a, c, then the weight-1 records as b, d.
More Python Programming questions
- 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?
- A solution loops over 2 x 10^5 queries, and each query runs if x in lst on a list of 2 x 10^5 items. Which change keeps the same answers…