ProgrammingHardCodeforces-style · placement coding round

What is printed?

What is printed?
from collections import Counter
def ways(a, t):
    dp = Counter({0: 1})
    for x in a:
        nd = Counter(dp)
        for s, c in dp.items():
            nd[s + x] += c
        dp = nd
    return dp[t]
print(ways([1, 2, 3], 3), ways([2, 2], 2))
  1. A. 2 1
  2. B. 1 2
  3. C. 2 2
  4. D. 3 2

Correct answer: C. 2 2

The counter stores how many index subsets reach each sum. For [1, 2, 3] the sum 3 comes from {3} and {1, 2}, and for [2, 2] there are two single-item choices of 2.

More Python Programming questions

Exams that ask this topic