In: Computer Science
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. FYI: this is Python Programming Language
Program Code Screenshot:
Sample Output:
The screenshots are attached below for reference.
Please follow them for proper indentation and output.
Program code to copy:
l=[]
while True:
print("Enter slot or -1 to stop")
temp=list(map(int,input().split()))#read time slots as tuples as
specified
if(temp[0]==-1):
break
l.append((temp[0],temp[1]))
res=[]
for i in range(1,len(l)):
if l[i][0]<l[i-1][1]:
continue
else:
if(l[i][0]>1200):
res.append(str(l[i][0]-1200)+"pm")#convert the times to 12 hr
format
if l[i-1][1]>1200:
res.append(str(l[i-1][1]-1200)+"pm")
else:
res.append(str(l[i-1][1])+"am")
else:
res.append(str(l[i][0])+"am")
if l[i-1][1]>1200:
res.append(str(l[i-1][1]-1200)+"pm")
else:
res.append(str(l[i-1][1])+"am")
res.append("600pm")#append the final time
if(l[-1][1]>1200):
res.append(str(l[-1][1]-1200)+"pm")#check the difference between
the final slot and final time
else:
res.append(str(l[-1][1])+"am")
for i in range(len(res)):
res[i]=res[i][:-4]+":"+res[i][-4:]
print("The available time slots are :") #print the slots
available
for i in range(len(res)-1,-1,-2):
print(res[i]," to ",res[i-1])