Take a Number
Implement a class TicketDesk for a waiting room in which people take numbered tickets and are called in the order of their numbers. Write these methods:
TicketDesk()opens the desk. Nobody is waiting yet.take()hands out the next ticket and returns its number. The first ticket is1, the next one2, and so on.call_next()calls the waiting person with the lowest ticket and returns that ticket number. That person is not waiting any more. If nobody is waiting, it returns-1.count_waiting()returns how many people are waiting.is_waiting(ticket)returnstrueif this ticket has been handed out and has not been called yet, andfalseotherwise.
The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
TicketDesk()
take() returns 1
take() returns 2
count_waiting() returns 2
call_next() returns 1
is_waiting(1) returns false
is_waiting(2) returns true
call_next() returns 2
call_next() returns -1
Tickets 1 and 2 are handed out, so two people wait. Ticket 1 is called: it is not waiting any more, but ticket 2 still is. Then ticket 2 is called, and after that nobody is left, so the last call returns -1.
Constraints
1 <= ticket <= 50- 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.
More Classes problems
- Tally CounterBeginner
- Thermostat Between Two LimitsBeginner
- Whose Turn Is ItBeginner
- Average of the Last K ReadingsEasy
- Call Limiter per UserEasy
- Clinic Waiting RoomEasy
- Coffee Stamp CardsEasy
- Coin Vending MachineEasy