ProgrammingHardTCS NQT · Wipro pattern

What is printed?

What is printed?
x = [3, 1, 2]
print(sorted(x, reverse=True)[:2], x[::-1][1:], list(reversed(sorted(x))))
  1. A. [3, 2] [1, 3] [3, 2, 1]
  2. B. [3, 1] [1, 3] [3, 2, 1]
  3. C. [3, 2] [2, 1] [1, 2, 3]
  4. D. [3, 2] [1, 3] [1, 2, 3]

Correct answer: A. [3, 2] [1, 3] [3, 2, 1]

sorted with reverse gives 3, 2, 1, so the first two are 3 and 2. x[::-1] is 2, 1, 3 and dropping the first gives 1, 3. reversed(sorted(x)) walks the ascending list backwards.

More Python Programming questions

Exams that ask this topic