ProgrammingVery hardTCS CodeVita · GATE CS · Advanced

What does the following Python 3 code print? import itertools print(list(itertools.combinations_with_replacement([1, 2, 3], 2)))

  1. A. [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
  2. B. [(1, 2), (1, 3), (2, 3)]
  3. C. [(1, 1), (2, 2), (3, 3)]
  4. D. [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]

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

Unlike plain combinations, an element may pair with itself, so (1,1),(2,2),(3,3) are included alongside the usual pairs, in sorted order.

More Python Programming questions

Exams that ask this topic