In: Computer Science
PYTHON
Generates a list of datetimes given a list of dates and a list of times. All possible combinations of date and time are contained within the result. The result is sorted in chronological order.
For example,
Input:
>>> timetable([date(2019,9,27), date(2019,9,30)],
[time(14,10), time(10,30)])
Output:
[datetime(2019,9,27,10,30), datetime(2019,9,27,14,10),
datetime(2019,9,30,10,30), datetime(2019,9,30,14,10)]
Current code:
from datetime import date, time, datetime
def timetable(dates, times):
lis = []
for c in times:
for y in dates:
x = f"(datetime{y},{c})"
lis.append(x)
#doesn't work at all
Screenshot of the code( Using Python 3.x version):
Sample Output:
Code to copy:
# Import the necessary libraries
from datetime import datetime
from datetime import date, time
# Define the function timetable with arguments as dates and times.
def timetable(dates: [], times: []):
# Initialize an empty list, lis
lis = []
# For each element in the list of dates.
for y in dates:
# For each element in the list of time.
for c in times:
# Append the empty list, lis with the elements of lists: dates, times
lis.append(datetime(y.year, y.month, y.day, c.hour, c.minute, c.second))
# Sort the list, lis
lis.sort()
# return lis.
return lis
# Taking the output from here.
print(timetable([date(2019,9,27), date(2019,9,30)], [time(14,10), time(10,30)]))