← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
You are tasked with building an appointment scheduling helper. Given an arbitrary datetime range from start_date to end_date, and a weekly repeating working_hours configuration table, generate every possible time slot for appointments during working hours within the given date range.
Given:
start_date: The start date of the datetime range (inclusive).end_date: The end date of the datetime range (inclusive).working_hours: A weekly repeating schedule of working hours, represented as a list of tuples. Each tuple contains two elements: the day of the week (0 for Sunday, 1 for Monday, ..., 6 for Saturday) and a string representing the working hours for that day in the format "HH:MM-HH:MM".Return:
start_date and end_date are in the format "YYYY-MM-DD".working_hours list contains at most 7 tuples, one for each day of the week.working_hours list is in the format "HH:MM-HH:MM", where HH is a two-digit hour (00-23) and MM is a two-digit minute (00-59).Given:
start_date: "2023-01-01"end_date: "2023-01-05"working_hours: [(0, "09:00-17:00"), (1, "10:00-18:00")]Return:
Given:
start_date: "2023-02-10"end_date: "2023-02-14"working_hours: [(0, "09:00-17:00"), (1, "10:00-18:00"), (2, "09:00-17:00"), (3, "10:00-18:00"), (4, "09:00-17:00"), (5, "10:00-18:00"), (6, "09:00-17:00")]Return:
start_date and end_date to datetime objects for easier manipulation.working_hours list to extract the start and end times for each day of the week.from datetime import datetime, timedelta
def generate_time_slots(start_date, end_date, working_hours):
# Convert start_date and end_date to datetime objects
start = datetime.strptime(start_date, "%Y-%m-%d")
end = datetime.strptime(end_date, "%Y-%m-%d")
# Parse working hours
working_hours_dict = {}
for day, hours in working_hours:
start_time, end_time = hours.split("-")
working_hours_dict[day] = (datetime.strptime(start_time, "%H:%M"), datetime.strptime(end_time, "%H:%M"))
# Generate time slots
time_slots = []
current_date = start
while current_date <= end:
day_of_week = current_date.weekday()
if day_of_week in working_hours_dict:
start_time, end_time = working_hours_dict[day_of_week]
current_time = start_time
while current_time <= end_time:
time_slots.append(current_date.strftime("%Y-%m-%dT") +