ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
print(sorted(['bb', 'a', 'cc', 'd', 'ee'], key=len, reverse=True))
- A. ['bb', 'cc', 'ee', 'a', 'd']
- B. ['ee', 'cc', 'bb', 'd', 'a']
- C. ['bb', 'cc', 'ee', 'd', 'a']
- D. ['cc', 'bb', 'ee', 'a', 'd']
Correct answer: A. ['bb', 'cc', 'ee', 'a', 'd']
reverse=True keeps equal keys in their original relative order (sort is stable in both directions). Reversing a sorted list instead would flip the ties to ['ee', 'cc', 'bb', 'd', 'a'].
More Python Programming questions
- What is printed? python import bisect a = [1, 2, 2, 2, 3] print(bisect.bisectleft(a, 2), bisect.bisectright(a, 2), bisect.bisectright(a…
- What is printed? python import bisect print(bisect.bisectleft([5, 4, 3], 4))
- What is printed? python import bisect print(bisect.bisectleft([(1, 'a'), (3, 'b'), (5, 'c')], 3, key=lambda t: t[0]))
- What is printed? python import bisect a = [(1, 'x'), (4, 'y')] bisect.insort(a, (3, 'z'), key=lambda t: t[0]) print(a)
- What is printed? python import heapq h = [1, 3, 5] r = heapq.heapreplace(h, 0) print(r, sorted(h))
- What is printed? python import heapq h = [1, 3, 5] r = heapq.heappushpop(h, 0) print(r, sorted(h))