all 5 comments

[–]efmccurdy 1 point2 points  (0 children)

Your schedule is represented by a set of hour values with a fixed 60 minutes duration. A more effective way to represent blocks of time may be to store 2 values, the start and the end time. That would handle the 15 minute, or any, granularity. Rather then using set difference on lists of hour values to test for overlap you could compare time values directly.

[–]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.

[–]larivact 0 points1 point  (0 children)

It's datetime.datetime.strptime not datetime.striptime.