263. Python Program to Make a Hospital Queue with Emergencies First
HardPhase 3 · Challenge
You know every tool here: lists, pop(0), a while loop and split(). It takes a few steps, so follow Example 1 on paper first.
Overview
A hospital emergency room does not serve people only in the order they came. Urgent cases go first. A program with two queues handles this, and the same idea runs help desks and print queues.
Problem Statement
Sana writes the queue screen for a small hospital. Read one command per line, until a line says end:
arrive <name> emergency: the patient joins the emergency queue. Print<name> waits (emergency).arrive <name> normal: the patient joins the normal queue. Print<name> waits (normal).arrive <name> <type>with any other type: printUnknown type <type>. The patient does not join any queue.next: the doctor sees the next patient. PrintDoctor sees <name> (emergency)orDoctor sees <name> (normal).
The rules for next:
- If anyone is in the emergency queue, the doctor sees the emergency patient who arrived first.
- If not, the doctor sees the normal patient who arrived first.
- If both queues are empty, print
No patients waiting.
After end, print Still waiting: and the names of everyone still waiting, with a comma and a space between them: first the emergency patients, then the normal ones, each in arrival order. If nobody is waiting, print Still waiting: nobody.
Examples
Example 1. Input:
arrive Ana normal
arrive Ben normal
arrive Chen emergency
next
next
arrive Dina emergency
next
end
gives:
Ana waits (normal)
Ben waits (normal)
Chen waits (emergency)
Doctor sees Chen (emergency)
Doctor sees Ana (normal)
Dina waits (emergency)
Doctor sees Dina (emergency)
Still waiting: Ben
Why: Chen came last but is an emergency, so the doctor sees Chen first. Dina also goes before Ben, who came much earlier.
Example 2. Input:
next
arrive Eli urgent
arrive Fay emergency
arrive Gus normal
arrive Hal emergency
end
gives:
No patients waiting
Unknown type urgent
Fay waits (emergency)
Gus waits (normal)
Hal waits (emergency)
Still waiting: Fay, Hal, Gus
Why: the first next finds nobody. urgent is not a type, so Eli does not join. At the end, both emergency patients come before Gus.
Input
One command per line, in the formats above, then a last line end. Names are single words. Types are case-sensitive: only emergency and normal in small letters are known. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
One line for each command, then the Still waiting: line. The tests try 7 different inputs, also an input with only end, only emergency patients, only normal patients, and the type Emergency with a capital E.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.