#๐Ÿ”’ Nested For Loop not following expected behaviour

29 messages ยท Page 1 of 1 (latest)

hushed parcel
#

Hi so I have this code for scheduling surgery appointments. Each surgery needs to be put on a day, in a time slot and be assigned a theatre.

Here's the requirements / constraints I have for the code:

  • There are 100+ surgeries
  • There are 9 time slots (read hours) in a ay for surgeries
  • That means that it will take 11_ days to do all of the surgeries
  • There are 3 operating theatres
  • There are 2 anaesthetists that can be used per day
  • A surgeon cannot perform two surgeries at the same time
  • A surgeon cannot perform two surgeries back-to-back
  • A surgeon can perform a maximum of their surgery quota in a day
  • A operating theatre can only be used for one surgery at a time

This is what I think needs to happen

  • The surgeons start their day in a resting state
  • The resting surgeons then move into the idle state
  • The working surgeons move into the resting state
  • The working surgeons then gets blanked

get_idle_surgeon

  • It's supposed to be checking if there are any idle surgeons then removes them
  • If there are no idle surgeons then it's checking that there are surgeons
  • if there are surgeons then it creates a new list and removes them form it (which doesn't make much sense now I think about it) when really it' supposed to be returning a surgeon for use in the loop
    Maybe it should be checking how many surgeons are in a resting state? and then providing one of those?

Code Examples
Surgeons List

[
    Surgeon(name='Meredith Gery', suregery_quota='4', surgery_quantity=0, in_surgery=False),
    Surgeon(name='Leonard McCoy', suregery_quota='3', surgery_quantity=0, in_surgery=False),
    Surgeon(name='Preston Burke', suregery_quota='2', surgery_quantity=0, in_surgery=False),
    Surgeon(name='Cristina Yang', suregery_quota='4', surgery_quantity=0, in_surgery=False),
    Surgeon(name='Beverly Crusher', suregery_quota='2', surgery_quantity=0, in_surgery=False)
]

Surgeries Data

[
    Surgery(surgery_type='Cholecystectomy', anesthetist_required='Yes'),
    Surgery(surgery_type='Broken Bone repair', anesthetist_required='Yes'),
    Surgery(surgery_type='Heart Bypass', anesthetist_required='Yes'),
    Surgery(surgery_type='Carpal Tunnel', anesthetist_required='No'),
    Surgery(surgery_type='Dupuytren Contracture Release', anesthetist_required='No')
]

Current Code
https://paste.pythondiscord.com/AFPA

desert egretBOT
#

@hushed parcel

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

little garnet
#

So, for me to understand better, do you want to make appointments or to create something like a schedule to optimize?

#

If only to make appointments, it could ask when the user wants to make that and make reservations for everything

hushed parcel
#

I'm looking to make a schedule of appointments/operations and store it in a list

little garnet
#

But if you wanna optimize, you may work with evolutive algorithm

#

Ok, you can create objects as threats, doctors, anesthesiologists and everything and save them in a database, with ids

#

When creating the appointment, save it as a list of lists like:
[Doctors, threatre, anesthesia]

#

I particularly like dictionaries

#

Or databases

#

With ids

#

Dude, really sorry, just now that I see you posted a code

#

Will read it

#

Ok

hushed parcel
#

Well I have these dataclasses to describe my different objects

@dataclass
class Surgeon:
    name: str
    suregery_quota: int
    surgery_quantity: int = 0
    in_surgery: bool = False

@dataclass
class Surgery:
    surgery_type: str
    anesthetist_required: bool

@dataclass
class Operation:
    day: int = None
    slot: int = None
    theatre: str = None
    surgeon: Surgeon = None
    surgery: Surgery = None

@dataclass
class Patient:
    age: int
    gender: str
    bmi: float
    children: int
    smoker: bool
    region: str
    medical_history: str
    family_medical_history: str
    exercise_frequency: str
    occupation: str
    coverage_level: str
    charges: float
    in_surgery: bool = False

A databse is a bit out of scope for what I'm looking for, at least at the moment. I was going to do a list of Operation, but maybe a dict would be better.
appointments is a int of the list of patients and the data inside it are all objects of Patient, this doesn't store an ID atm but it reads from a pandas dataframe so adding an ID won't be a huge hassle

little garnet
#

But, what do you want to do?

#

What is the actual question?

hushed parcel
#

I'm struggling to articulate it, give me a min

little garnet
#

Inside the objects doctors, theatres and etc you can save the hours it will be free

#

Then remove when when on work and the resting time

hushed parcel
#
  • I want to make a list of Operation objects.
  • Each Operation needs to be assigned a day - this is calculated from the number of patients divided by the number of time slots
  • Each Operation needs to be assigned a time slot - There are 9 time slots per day
  • Each Operation needs to be assigned a theatre - there are 3 theatres
  • Each Operation needs to be assigned a surgeon - there are 5 surgeons
  • Each Operation needs to be assigned a surgery.

My issue at the moment is that my code is producing a index error

 Resting Surgeons 01: []
 Working Surgeons 01: []
 Idle Surgeons 01: []
Day: 1
Slot: 1
 Resting Surgeons 02: []
 Working Surgeons 02: []
 Idle Surgeons 02: []
 Idle Surgeons 03: []
 Resting Surgeons 03: []
Theatre: 1
{
    "name": "IndexError",
    "message": "pop from empty list",
    "stack": "---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[14], line 76
     74 for theatre in range(1, theatres + 1):
     75     print(f\"Theatre: {theatre}\")
---> 76     surgery = get_next_surgery(surgeries)
     77     print(f\"Surgery: {surgery}\")
     78     surgeon = get_idle_surgeon(idle_surgeons, surgeons)

Cell In[14], line 39, in get_next_surgery(surgeries)
     38 def get_next_surgery(surgeries):
---> 39     surgery = surgeries.pop()
     40     return surgery

IndexError: pop from empty list"
}

Which is caused by the lists being empty and not being assigned any values

little garnet
#

To save it or just for when the program is running?

hushed parcel
#

I only need it to store it while the program is running, I don't need to store it in a file or db

little garnet
#

You can also make a if statement to check if the list is empty

hushed parcel
#

I do have that, in get_idle_surgeon, but I presume my logic isn't wokring correctly

#

My latest idea was to do it in pieces like this

def get_day(appointments, operations, time_slots):
    # calculate the number of days
    days = appointments // time_slots
    # iterate over the operations
    for operation in operations:
        # skip if the day is already set
        if operation.day != None:
            return
        # calculate the day of the operation
        for day in range(1, days + 1):
            # remove the number of time slots from the appointments
            # Not sure if this is needed
            appointments -= time_slots
            # set the day of the operation
            operation.append(Operation(day, None, None, None, None))

def get_slot(appointments, operations, time_slots):
    # iterate over the operations
    for operation in operations:
        # skip if the slot is already set
        if operation.slot == None:
            # calculate the slot of the operation
            operation.slot = appointments % time_slots
            # remove the number of time slots from the appointments
            appointments -= time_slots

def get_theatre(operations, theatres):
    # iterate over the operations
    for operation in operations:
        # skip if the theatre is already set
        if operation.theatre != None:
            return
        for theatre in range(1, theatres + 1):
            # set the theatre of the operation
            operation.theatre = theatre

def get_surgery(operations, surgeries):
    # iterate over the operations
    for operation in operations:
        # skip if the surgery is already set
        if operation.surgery != None:
            return
        for surgery in surgeries:
            # set the surgery of the operation
            operation.surgery = surgery
#

But that seems to be a moot point because my issue at the moment is figuring out what state the surgeon is in.
And I'm not sure how these would help me enforce the contraints

desert egretBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.