FunctionRacerProblemsRace

Register That Gives the Fewest Coins

HardClassesPython, JavaScript, TypeScript, Java, C++

Implement a class CashRegister that sells items and gives change from the coins in its drawer. Write these methods:

The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.

Example 1

CashRegister([1,5,10,25], [0,0,5,2])
sell(70, [0,0,0,4]) returns [0,0,3,0]
read_drawer() returns [0,0,2,6]
read_total() returns 170
sell(15, [0,0,1,0]) returns []
sell(5, [0,0,1,0]) returns []
read_drawer() returns [0,0,2,6]

The 30 cents of change cannot start with a quarter, because no nickels or pennies are left for the other 5. Three dimes work. The second sale is underpaid, and the third needs 5 cents of change that the drawer cannot make, so both are refused.

Example 2

CashRegister([1,2,3], [2,2,2])
sell(2, [0,0,2]) returns [1,0,1]
read_drawer() returns [1,2,3]

The 4 of change can be 3 + 1 or 2 + 2, two coins either way. The register prefers more coins of the largest value, so it gives 3 + 1.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Classes problems