Embercode
✕
Dynamic ProgrammingQuick quiz

Subset sums

8 questions. Covers Target sum by transforming to subset sum in Python.

Questions 1 to 5 of 8

  1. 1. Why can the program answer no straight away when the total of the numbers is odd?
  2. 2. The list is five ones, 1, 1, 1, 1, 1, and the target is 3. Which value is p, the sum the chosen subset must reach?
  3. 3. The bitset version of the subset sum table needs no backwards loop, because reach << x reads the old value before the | stores the new one.
  4. 4. A mask built as (1 << target) - 1 instead of (1 << (target + 1)) - 1 is just as correct for the bitset method.
  5. 5. What does this program print? ``python def count_ways(nums, target): total = sum(nums) if abs(target) > total or (total + target) % 2 != 0: return 0 p = (total + target) // 2 ways = [0] * (p + 1) ways[0] = 1 for x in nums: for s in range(p, x - 1, -1): ways[s] += ways[s - x] return ways[p] print(count_ways([2, 3, 5], 1)) ``

5 question(s) left on this page.