Question

In: Computer Science

Assuming you are write a program for the emergency room. You maintain two queues. A regular...

Assuming you are write a program for the emergency room. You maintain two queues. A regular FIFO queue for doctors. When a doctor walked in, he/she will join this queue if there is no patients waiting.
Otherwise, remove the patient with the highest priority and let the doctor treat this patient.
A priority queue for patients. When a patient walked, input patients name and priority. If doctor’s queue is not empty, remove the first doctor and let this doctor server this patients. Otherwise add this patient with the priority to the priority queue.
PYTHON

Solutions

Expert Solution

Code tested in Python 3.6.9 in the Linux operating system.

Idea

We will use 2 modules available in Python to solve this problem

  1. deque: deque is short for "double-ended queue". It supports add and remove operations at both front and back. It can be used to implement a stack or a queue. We'll use it to implement the doctors queue.
  2. heapq: heapq provides us the implementation of a priority queue that we can use to add patients or remove patients.

Helpful and self-explanatory comments are added to the code and output is also attached.

Code

from collections import deque
from heapq import heappush, heappop

class Doctor:
  def __init__(self, id):
    self.id = id

class Patient:
  def __init__(self, name, priority):
    self.name = name
    self.priority = priority
  # Comparator method for priority queue
  def __lt__(self, other):
    if self.priority == other.priority:
      # If priority is same, take element with lexographically shorter name first
      return self.name <= other.name
    # Else, take element with higher priorty first
    return self.priority > other.priority

doctors_queue = deque()
waiting_list = [] # For patients

# Series of events
events = [
  Patient('John', 10), # (Patient Name, Priority)
  Patient('Wick', 20),
  Doctor(1), # (DoctorID)
  Doctor(2),
  Doctor(3),
  Doctor(4),
  Patient('Lorem', 2),
  Patient('Epsum', 1),
  Patient('EFD', 1),
  Patient('ABC', 1),
  Doctor(5),
]

# Process events
for e in events:
  # Check if a doctor arrived
  if isinstance(e, Doctor):
    print('Doctor {} arrived'.format(e.id))
    # Check if a patient is waiting
    if len(waiting_list) > 0:
      # Pop the highest priority patient from waiting list
      patient = heappop(waiting_list)
      print('Patient [{}] with priority = {} assigned to Doctor {}'.format(patient.name, patient.priority, e.id))
    else:
      # No patient waiting, join the queue
      doctors_queue.append(e)
      print('Doctor {} joined the queue'.format(e.id))
  else:
    print('Patient [{}] with priority = {} arrived'.format(e.name, e.priority))
    # Check if a doctor is available
    if len(doctors_queue) > 0:
      # If yes, assign patient to this doctor
      doctor = doctors_queue.popleft()
      print('Patient [{}] with priority = {} assigned to Doctor {}'.format(e.name, e.priority, doctor.id))
    else:
      # Otherwise, add patient to priority queue
      heappush(waiting_list, e)
      print('Patient [{}] joined the queue'.format(e.name))

Output


Related Solutions

For this problem, you will write a program using two queues. Generate n random numbers between...
For this problem, you will write a program using two queues. Generate n random numbers between 10 and 100 (both inclusive), where n>9. The value of n should be taken as input from the user and n should be >9. The numbers could be duplicated. Enqueue all these numbers to the first queue. The objective is to find the numbers whose sum of digits is odd and enqueue them to the second queue. The remaining numbers (whose sum of digits...
It is a busy night in the emergency room and the ICU is down to two...
It is a busy night in the emergency room and the ICU is down to two open beds. At two in the morning, the ambulance pulls up to the emergency room door with three patients who had been involved in a shoot-out. Apparently, a sting operation had gone bad during a drug deal, and an undercover policeman and two drug dealers had been wounded. All three men appear to be in critical condition and need the services of the ICU...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct...
1. Maintain two arrays for use in “translating” regular text to “leetspeak”: (a) One array will...
1. Maintain two arrays for use in “translating” regular text to “leetspeak”: (a) One array will contain the standard characters: char[] letters = {'a', 'e', 'l', 'o', 's', 't'}; (b) The other will contain the corresponding symbols: char[] symbols = {'@', '3', '1', '0', '$', '7'}; 2. Prompt the user to enter a full sentence with all lower-case characters, then store it in a variable to be used later. (a) You can (optionally) manually convert the string to all lower-case...
A 45 year old man presents in the Emergency Room with a two day history of...
A 45 year old man presents in the Emergency Room with a two day history of black covered stools and recurrent nosebleeds. His history reveals both a recent as well as past history of ethanol abuse. His coagulations studies reveal: Platelet Count:                       60,000/cumm PT:                                          20 sec APPT:                                      52 sec Thrombin Time:                    11 sec Fibrinogen:                            201 mg% FDP                                         greater than 40 ?g.ml 1. What is the probable diagnosis? 2. What additional tests should be performed to confirm the diagnosis?
Two physicians are finishing their rounds when a paramedic brings a patient in the emergency room....
Two physicians are finishing their rounds when a paramedic brings a patient in the emergency room. The senior paramedic, Jim Morrison, reported that the patient was swimming at the local quarry and did a forward flip into the water, striking some submerged rocks. "Which part of his body struck the rocks?" asked Dr. Carter. "He was in a hyperflexed-tucked-position when he hit the rocks, lacerating the right side of his head and neck and upper back. The patient indicated he...
I am to write a code that uses two queues, and print the generated random numbers...
I am to write a code that uses two queues, and print the generated random numbers and content of both queues. I need to use the enqueue and dequeue functions in the code.  The instructions are below. Program should be in C Instructions: Generate n random numbers with values between 10 - 100. Note: n>9 if u write a function (for example, called generateRand) to do this - what is the data or input we have to give it? Create a...
On one particular day, two patients were brought in to a local emergency room, each with...
On one particular day, two patients were brought in to a local emergency room, each with different signs and different prognoses, but each with the same underlying disease, stroke. The first patient, a 70-year-old male, arrived by ambulance and was accompanied by his elderly wife. The patient was awake and alert, but was unable to move independently due to paralysis on his right side. He looked responsive and seemed to understand what was said to him, but he could not...
You are seeing a young previously healthy patient in the Emergency room with an acute asthma...
You are seeing a young previously healthy patient in the Emergency room with an acute asthma attack. She looks very uncomfortable, she has diffuse wheezing throughout all lung fields and is requiring 50% oxygen by face mask to maintain oxygen saturations above 90%. A nurse reports that when the patient blows in her peak flow meter it reads in the normal range. What should you do next? a. Send the patient home. She is better b. Repeat the peak flow...
You saw a colleague of yours at the psych emergency room waiting to be seen. He...
You saw a colleague of yours at the psych emergency room waiting to be seen. He said that he came to see a colleague who is hospitalized. Why do you think he responded this way? What is your perspective on this issue?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT