Search code examples
pythonalgorithmcomparisonschedulebetween

Schedule check python without module


guys, I am trying to study basic algorithm about python.

What I am trying to figure out for now is to create the schedule.

I want to ask a very basic question. I can't figure this out how to do it.

My question is,

Suppose if I have two schedule, which can be 2021-04-07 19:00 to 2021-04-07 20:00, and 2021-04-08 19:00 to 2021-04-08 20:00 in my schedule (can be dictionary or list)

What I am trying to do is that, when I add new schedule to my scheduler (list or dic), if I have a schedule, I want to raise ValueError.

But, I have no idea how to compare the schedule.

I don't want to use module for easy, I am just studying fundamental!

For example,

MySchedule = {'dinner': [2021-04-07 19:00, 2021-04-07 20:00], 'dinner': [2021-04-08 19:00, 2021-04-08 20:00]}

I want to add a new schedule which can be 'exercise': [2021-04-07 18:00, 2021-04-07 21:00], but I already have a schedule at that time.

Then, I want to raise a ValueError,

Is there any best way to check that I have a schedule at the time?

Do I need to change the date and time to int, and compare with new schedule time?

If possible, anyone give me a basic idea with it :(?

Thank you guys for reading!


Solution

  • Compare the start and end time of the new event to the start and end time of each event in the schedule.

    If the new event starts and ends before the scheduled event, or starts and ends after the scheduled event, then there is no conflict with the scheduled event. Otherwise there is a conflict.

    If you make it through all the scheduled events without finding a conflict, then the new event may be added to the schedule.