ProgrammingHardProduct-company MCQ pattern

What is printed on the first line?

What is printed on the first line?
data = [('x', 3.0), ('y', 3), ('z', 2.5)]
print(sorted(data, key=lambda t: t[1]))
print(max(data, key=lambda t: t[1]), min(data, key=lambda t: t[1])[0])
  1. A. [('z', 2.5), ('y', 3), ('x', 3.0)]
  2. B. [('z', 2.5), ('x', 3.0), ('y', 3)]
  3. C. [('x', 3.0), ('y', 3), ('z', 2.5)]
  4. D. [('z', 2.5), ('x', 3), ('y', 3.0)]

Correct answer: B. [('z', 2.5), ('x', 3.0), ('y', 3)]

3.0 and 3 compare equal, and the stable sort keeps 'x' ahead of 'y'. The smallest value is 2.5, so the first line starts with z.

More Python Programming questions

Exams that ask this topic