ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
print(sorted([(-1, 'a'), (1, 'b'), (-1, 'c')], key=lambda t: t[0], reverse=True))
  1. A. [(-1, 'c'), (-1, 'a'), (1, 'b')]
  2. B. [(1, 'b'), (-1, 'c'), (-1, 'a')]
  3. C. [(1, 'b'), (-1, 'a'), (-1, 'c')]
  4. D. [(-1, 'a'), (1, 'b'), (-1, 'c')]

Correct answer: C. [(1, 'b'), (-1, 'a'), (-1, 'c')]

Reverse order puts 1 first. The two -1 entries tie, and sorted keeps tied items in their input order, so a comes before c.

More Python Programming questions

Exams that ask this topic