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))- A. 2 1
- B. 1 2
- C. 2 2
- 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
- What is printed? python print(bin(5 ^ 3), int('101', 2) << 2, ~5, -~5)
- What is printed? python import math print(math.prod([]), math.prod([2, 3.0]))
- What is printed? python import heapq h = [1, 3] heapq.heapify(h) print(heapq.heapreplace(h, 0), sorted(h), heapq.heappushpop(h, 0)…
- What is printed? python try: print(list(zip([1, 2], [3], strict=True))) except ValueError: print('strict')
- What is printed? python print(round(1234, -2), round(1250, -2), round(1350, -2))
- What is printed? python def lcsub(a, b): best = end = 0 dp = [[0] (len(b) + 1) for in range(len(a) + 1)] for i in range(1, len(a) + 1)…