Predict the output
What does this print?
weights, cap = [2], 4
up = [True] + [False] * cap
for w in weights:
for t in range(w, cap + 1):
up[t] = up[t] or up[t - w]
down = [True] + [False] * cap
for w in weights:
for t in range(cap, w - 1, -1):
down[t] = down[t] or down[t - w]
print(up[4], down[4])Choose an answer, then open Show answer below to check it.
Show answer
Answer: A True False
Right: the upward sweep used the single weight 2 twice to reach 4.
Fill direction is what separates 'unlimited copies' from 'each item once' when the table is a single row.
Why not the others?
- B False False. Upward sweeps read values already updated for this item.
- C True True. Sweeping downward uses each item at most once, so 4 is not reachable.