ProgrammingHardTCS NQT · Python

What is printed?

What is printed?
pts = [(2, 1), (1, 5), (2, 0), (1, 2)]
print(sorted(pts, key=lambda p: (-p[0], p[1])), max(pts, key=lambda p: p[1]))
  1. A. [(2, 0), (2, 1), (1, 2), (1, 5)] (2, 1)
  2. B. [(2, 1), (2, 0), (1, 5), (1, 2)] (1, 5)
  3. C. [(1, 2), (1, 5), (2, 0), (2, 1)] (1, 5)
  4. D. [(2, 0), (2, 1), (1, 2), (1, 5)] (1, 5)

Correct answer: D. [(2, 0), (2, 1), (1, 2), (1, 5)] (1, 5)

The key (-first, second) sorts first by descending first element, then ascending second; max picks the pair with the largest second element.

More Python Programming questions

Exams that ask this topic