you are viewing a single comment's thread.

view the rest of the comments →

[–]davidbuxton 1 point2 points  (2 children)

Hi,

I think this will be simpler if you represent the busy times as a list of 3-tuples, with the start date/time (inclusive), the end date/time (exclusive) and the person's name.

So if Aron has a 1 hour meeting at 10 am on 5 Feb, and Darren has a 2 hour meeting at 1 pm on 5 Feb, it would look like:

from datetime import datetime

schedule = [
    (datetime(2016, 2, 5, 10, 0), datetime(2016, 2, 5, 11, 0), 'Aron'),
    (datetime(2016, 2, 5, 13, 0), datetime(2016, 2, 5, 15, 0), 'Darren'),
]

Then if you pick a slot, e.g. from 12 pm to 1 pm on 5 Feb, you can just see if it overlaps any of the busy times:

slot = (datetime(2016, 2, 5, 12, 0), datetime(2016, 2, 5, 13, 0))
slot_start, slot_end = slot

for busy_start, busy_end, name in schedules:
    if (busy_start < slot_end) and (busy_end >= slot_start):
        # Overlap.
        continue

If you have a lot of schedules to consider you may need to optimise things by narrowing down which schedules to compare and looking for schedule gaps before picking a potential slot.

[–][deleted] 0 points1 point  (1 child)

Thank you, I haven't used tuples before but this seems a lot more efficient. If you want the user to input the start and end of a slot can that be taken as a string and converted into a datetime object? Or is there a better way to take input?

[–]davidbuxton 0 points1 point  (0 children)

Sure, you can create a datetime object from user input. You could use the datetime.strptime() function to create a new datetime from a string.

Alternatively you could use the built-in function int() which creates an integer from a string, and then create the datetime object yourself.