CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List

Select topics to narrow your question pool, then enable Random to jump to a random question matching your filters.

Topics:
Python Quiz #47

When Greedy Change Fails

hardData Structures & Algorithms

Greedy change-making takes the largest coin that still fits at every step. With coins worth 1, 3 and 4, what does the greedy program below produce for 6, and what does that show?

📄 Code

1coins: list[int] = [4, 3, 1]
2amount: int = 6
3used: list[int] = []
4for coin in coins:
5 while amount >= coin:
6 used.append(coin)
7 amount = amount - coin
8print(used)