Predict the output
What does this print?
weights = [1, 5, 11, 5]
reach = 1 # bit t set: total t is reachable
for w in weights:
reach |= reach << w
half = sum(weights) // 2
print(half, bool(reach >> half & 1))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 11 True
Right: 11 alone matches 1 + 5 + 5.
Each shift adds one weight to every total reached so far, so one integer stands in for the whole table.
Why not the others?
- B 11 False. Total 11 is reachable with the single item 11.
- C 22 True. Half of 22 is 11.