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)])
  1. A. ['c', 'a', 'd', 'b']
  2. B. ['a', 'c', 'd', 'b']
  3. C. ['d', 'b', 'c', 'a']
  4. 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

Exams that ask this topic