ProgrammingVery hardProduct-company MCQ pattern
What is printed on the second line?
What is printed on the second line?
data = [('b', 2), ('a', 2), ('c', 1), ('a', 1)]
print(sorted(data, key=lambda t: (t[1], t[0]), reverse=True))
print(sorted(sorted(data, key=lambda t: t[0]), key=lambda t: t[1], reverse=True))- A. [('a', 2), ('b', 2), ('c', 1), ('a', 1)]
- B. [('b', 2), ('a', 2), ('a', 1), ('c', 1)]
- C. [('a', 2), ('b', 2), ('a', 1), ('c', 1)]
- D. [('a', 1), ('c', 1), ('a', 2), ('b', 2)]
Correct answer: C. [('a', 2), ('b', 2), ('a', 1), ('c', 1)]
Sorting by name first and then stably by the number (descending) keeps names in alphabetical order inside each number group. Two stable sorts give the same result as one sort on a tuple key.
More Python Programming questions
- What is printed on the second line? python words = ['bb', 'a', 'ccc', 'dd', 'e'] print(sorted(words, key=len)) print(sorted(words…
- What is printed on the last line? python nums = ['10', '9', '100', '1'] print(sorted(nums)) print(sorted(nums, key=int)) print(max(nums)…
- What is printed on the third line? python print(sorted([(1, 'x'), (1, 'X'), (0, 'z')], key=lambda t: t[1].lower())) print(sorted(['b'…
- What is printed on the second line? python u = '' for ch in 'abc': u = ch + u print(u)
- What is printed on the first line? python x = 0.1 3 y = 0.3 print(abs(x - y) < 1e-9, x > y, x - y, y - x) print(float('0.1') 3 == 0.3, 3…
- What is printed on the second line? python import math print(math.isclose(1e9, 1e9 + 1e-3), math.isclose(1e9, 1e9 + 1, reltol=1e-9))…