Submit Python code for the following questions: You have the following appointment slots booked during the day: 8am - 10am, 11am - 12pm, 1:30pm - 3pm, 2pm - 5pm. Write a program that takes as input a...

1 answer below »

Submit Python code for the following questions:



  1. You have the following appointment slots booked during the day: 8am - 10am, 11am - 12pm, 1:30pm - 3pm, 2pm - 5pm. Write a program that takes as input a list of booked appointment slots, and returns slots when you are free. You solution should be generalized to work with any valid input.

    • Assumptions:

      • Your day starts at 8am and ends at 6pm.

      • You can have overlapping booked appointments.



    • Hints/suggestions:

      • Use military time so 1:30pm is represented as 1330, for example.

      • Use a tuple (800,1000) to represent time slot 8am - 10am.





  2. Define a recursive function to sort an input list of numbers.Do not use inbuilt Python sorting functions.Hints:

    • Use min() to get the smallest number in the list, then recursively call the function on the remaining numbers in the list to get the next smallest number. Note that the same number can appear more than once in the list.

    • Use list comprehensions to make your code more concise, e.g.[x for x in my_list if x == max_val]returns all values in a listmy_listwhose value is equal tomax_val.






# Example: booked 9-10am, 3-4pm, 3:30 - 5:30pm # assume slots are sorted by (start time, end time), # i.e., for slots with the same start time, sort by end time booked = [(900, 1000), (1500, 1600), (1530, 1730)] # start with completely free day 8am - 6pm day = (800, 1800) # this will be the resulting array of free slots free = [] # get free slot at beginning of day # TODO: compare the start times of the day and the first time slot # get free in-between slots for i in range(len(booked)): # TODO: if the current time slot has a start time # greater than the previous slot's end time, create a free slot # get free slot at end of day # TODO: compare the end times of the last time slot and the day # print free slots print('Free slots', free) # TODO: create a general function with the above logic # TODO: if the input slots are not sorted, how would you do so? # https://docs.python.org/3/howto/sorting.html#key-functions
Answered Same DayOct 17, 2021

Answer To: Submit Python code for the following questions: You have the following appointment slots booked...

Shivani answered on Oct 18 2021
158 Votes
# Example: booked 9-10am, 3-4pm, 3:30 - 5:30pm
# assume slots are sorted by (start time, end time),

# i.e., for slots with the same start time, sort by end time
booked = [(900, 1000), (1500, 1600), (1530, 1730)]
# start with completely free day 8am - 6pm
day = (800, 1800)
# this will be the resulting array of free slots
free = []
# get free slot at beginning of day
# TODO: compare the start times of the day and the first time slot
#print(booked[0][0])
if (booked[0][0] > day[0]):
free_slot = (day[0],booked[0][0])
free.append(free_slot)
# get free in-between slots
for i in range(len(booked)-1):
# TODO: if the current time slot has a start time
# greater than the previous slot's end time,...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here