Predict the output
What does this print?
coins, amount = [1, 2], 4
combos = [1] + [0] * amount
for c in coins:
for a in range(c, amount + 1):
combos[a] += combos[a - c]
orders = [1] + [0] * amount
for t in range(1, amount + 1): # totals outside
for step in coins:
if step <= t:
orders[t] += orders[t - step]
print(combos[amount], orders[amount])Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 3 5
Right: {1111, 112, 22} as combinations, but five orderings.
Same table and same additions; only the loop order decides whether order matters.
Why not the others?
- B 5 3. The coin-outside loop is the one that counts combinations.
- C 3 3. Orderings count 1 + 1 + 2, 1 + 2 + 1 and 2 + 1 + 1 separately.