Skip to content

Instantly share code, notes, and snippets.

@ezirmusitua
Last active January 17, 2019 15:27
Show Gist options
  • Save ezirmusitua/83fd23dd7fc61c707add0441b906a307 to your computer and use it in GitHub Desktop.
Save ezirmusitua/83fd23dd7fc61c707add0441b906a307 to your computer and use it in GitHub Desktop.
[Schedule daily appointment] codewar challenge: schedule daily appointment #algorithm #challenge #python
Total_Work_Time_In_Minute = (19 - 9) * 60 + 1
def timestr2minute(timestr):
hour, minute = map(lambda v: int(v), timestr.split(':'))
return hour * 60 + minute
def minute2timestr(point_in_sheet):
hour, minute = divmod(point_in_sheet, 60)
hour_str, minute_str = '', ''
hour_str = '0' + str(hour + 9) if hour == 0 else str(hour + 9)
minute_str = '0' + str(minute) if minute <= 9 else str(minute)
return hour_str + ':' + minute_str
def generate_ap_timesheet(start_timestr, end_timestr):
start = timestr2minute(start_timestr)
end = timestr2minute(end_timestr)
return [1 for i in range(0, end - start)]
def generate_timesheet(schedule):
timesheet = [0 for i in range(0, Total_Work_Time_In_Minute)]
for ap in schedule:
ap_start, ap_end = map(lambda x: timestr2minute(x) - timestr2minute('09:00'), ap)
timesheet = timesheet[:ap_start] + [1 for i in range(0, ap_end - ap_start)] + timesheet[ap_end:]
return timesheet
def check_free_in_period(timesheet, start, duration):
return timesheet[start:start + duration] == [0 for i in range(0, duration)]
def get_start_time(schedules, duration):
timesheets = [generate_timesheet(schedule) for schedule in schedules]
for point in range(0, Total_Work_Time_In_Minute - duration):
if len(list(filter(lambda t: check_free_in_period(t, point, duration), timesheets))) is len(timesheets):
return minute2timestr(point)
return None
schedules = [
[['09:09', '11:27'], ['12:14', '13:41'], ['15:16', '17:17'], ['17:32', '18:50']],
[['10:38', '12:06'], ['13:39', '15:08'], ['17:23', '17:26'], ['18:02', '18:26']]
]
print(get_start_time(schedules, 10) == '18:50')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment