120k views
5 votes
Complete the change method of the ChangeMachine class. A ChangeMachine instance holds some coins, which are initially all pennies. The change method takes a positive integer coin, adds that coin to its coins, and then returns a list that sums to coin. The machine prefers to return as many of the smallest coins available, ordered from smallest to largest. The coins returned by change are removed from the machine's coins. Hint: Call the make_change function in order to compute the result of , but update before returning that result. Hint: To remove key-value pairs from a dictionary, you can use use . pop () . For example, d.pop("first_key") will remove the key-value pair associated with "first_key" from d. class ChangeMachine: "I"'A change machine holds a certain number of coins, initially all pennies. The change method adds a single coin of some denomination X and returns a list of coins that sums to X. The machine prefers to return the smallest coins available. The total value in the machine never changes, and it can always make change for any coin (perhaps by returning the coin passed in). The coins attribute is a dictionary with keys that are positive integer denominations and values that are positive integer coin counts. ≫>= ChangeMachine (2) ≫m. coins =={1:2} True ≫m. change (2) [1,1] ≫ m. coins =={2:1} True ≫m. change (2) [2] ≫m. coins =={2:1} True ≫m. change ( 3 ) [3] ≫m⋅ coins =={2:1} True ≫m= ChangeMachine (10)#10 pennies ≫m⋅ coins =={1:10} True ≫ m.change(5) \# takes a nickel \& returns 5 pennies [1,1,1,1,1] ≫ m. coins =={1:5,5:1}#5 pennies \& a nickel remain True ≫m. change ( 3 ) [1,1,1] ≫m⋅ coins =={1:2,3:1,5:1} "*** YOUR CODE HERE ***"

User Carolanne
by
7.4k points

1 Answer

2 votes

Final answer:

The given question is asking to complete the change method of the ChangeMachine class. The method takes a positive integer coin, adds that coin to its coins, and returns a list that sums to coin. The machine prefers to return as many of the smallest coins available, ordered from smallest to largest.

Step-by-step explanation:

The given question is asking to complete the change method of the ChangeMachine class. The change method takes a positive integer coin, adds that coin to its coins, and returns a list that sums to coin. The machine prefers to return as many of the smallest coins available, ordered from smallest to largest. In order to solve this problem, we can iterate through the coins dictionary and calculate the total value of all coins. Then, we can subtract the given coin from the total value and return a list of coins that sums to the given coin.

Here is the completed change method:

def change(self, coin):
result = []
total_value = sum(self.coins.keys())
remaining_value = total_value - coin
if remaining_value < 0:
return result
for denomination in sorted(self.coins.keys(), reverse=True):
while denomination <= remaining_value:
result.append(denomination)
self.coins[denomination] -= 1
remaining_value -= denomination
return result

User Bassfader
by
8.0k points