Untitled
unknown
python
2 years ago
2.0 kB
9
Indexable
from datetime import datetime, timedelta
start_time = datetime.strptime('09:00', '%H:%M')
end_time = datetime.strptime('22:00', '%H:%M')
busy = [
{'start': '10:30', 'stop': '10:50'},
{'start': '18:40', 'stop': '18:50'},
{'start': '14:40', 'stop': '15:50'},
{'start': '16:40', 'stop': '17:20'},
{'start': '20:05', 'stop': '20:20'}
]
busy_intervals = []
for interval in busy:
start = datetime.strptime(interval['start'], '%H:%M')
stop_time = datetime.strptime(interval['stop'], '%H:%M')
busy_intervals.append((start, stop_time))
def _is_busy_time(start_t: datetime, busy_frame: tuple[datetime, datetime]) -> bool:
stop_t = start_t + timedelta(minutes=30)
if busy_frame[0] <= stop_t <= busy_frame[1]:
return True
elif start_t <= busy_frame[0] <= stop_t:
return True
elif start_t == busy_frame[0]:
return True
return False
free_windows = []
current_time = start_time
while current_time + timedelta(minutes=30) < end_time:
# Заполним время с учётом занятых окон
for frame in sorted(busy_intervals, key=lambda x: x[0]):
while not _is_busy_time(current_time, frame):
stop_time = current_time + timedelta(minutes=30)
free_time = {'start': current_time, 'stop': stop_time}
free_windows.append(free_time)
current_time = stop_time
current_time = frame[1]
# Заполним оставшееся рабочее время
while current_time + timedelta(minutes=30) < end_time:
stop_time = current_time + timedelta(minutes=30)
free_time = {'start': current_time, 'stop': stop_time}
free_windows.append(free_time)
current_time = stop_time
# Вывод списка свободных окон
for window in free_windows:
print(f'Doctor`s free time is: {window["start"].strftime("%H:%M")} -- {window["stop"].strftime("%H:%M")}')
Editor is loading...
Leave a Comment