Coin Vending Machine
Implement a class VendingMachine that sells snacks from numbered slots and takes coins. Write these methods:
VendingMachine(prices, stock)sets up the machine. Slotisells a snack forprices[i]cents and starts withstock[i]snacks. The credit starts at0.insert_coin(coin)takes a coin worthcoincents. Only5,10and25cent coins are accepted: an accepted coin is added to the credit and the method returns the new credit. Any other coin is handed back: the credit stays the same and the method returns-1.buy(slot)sells one snack fromslot. If the slot is empty, nothing changes and it returns-1. Otherwise, if the credit is less than the price, nothing changes and it returns-2. Otherwise one snack leaves the slot, the price is taken off the credit, and the method returns the credit that is left (which stays in the machine for the next purchase).refund()hands back the whole credit: it returns the credit and sets it to0.snacks_left(slot)returns how many snacks are left inslot.
slot is always a valid slot number. The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
VendingMachine([35,50], [2,0])
insert_coin(25) returns 25
buy(0) returns -2
insert_coin(10) returns 35
buy(0) returns 0
buy(1) returns -1
insert_coin(3) returns -1
insert_coin(25) returns 25
refund() returns 25
snacks_left(0) returns 1
25 cents is not enough for the 35 cent snack, so the first purchase gives -2. After a 10 the credit is exactly 35 and the sale leaves 0. Slot 1 is empty, a 3 cent coin is handed back, and the last 25 is refunded.
Constraints
1 <= len(prices) == len(stock) <= 4- Every price is a multiple of
5from5to100, and0 <= stock[i] <= 4. 1 <= coin <= 100- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.