ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
from itertools import combinations
print(sum(1 for r in range(5) for c in combinations([1, 2, 3, 4], r) if sum(c) == 5))
  1. A. 1
  2. B. 3
  3. C. 4
  4. D. 2

Correct answer: D. 2

The subsets with sum 5 are {1, 4} and {2, 3}. Every other subset of these numbers sums to a different value.

More Python Programming questions

Exams that ask this topic