Question

In: Computer Science

PYTHON    Generates a list of datetimes given a list of dates and a list of...

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

Solutions

Expert Solution

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)]))


Related Solutions

Given a list of items, write a program that generates a list of lists of the...
Given a list of items, write a program that generates a list of lists of the following form: [a,b,c,...,z]⇒[[z], [y,z], [x,y,z], ... , [a,b, ... ,y,z]] Hint: Slicing is your friend. please write a python program
Write the python code that generates a normal sample with given μ and σ, and the...
Write the python code that generates a normal sample with given μ and σ, and the code that calculates m (sample mean) and s (sample standard deviation) from the sample.
Write the python code that generates a normal sample with given μ and σ, and the...
Write the python code that generates a normal sample with given μ and σ, and the code that calculates m and s from the sample. Do the same using the Bayes’ estimator assuming a prior distribution for μ.
The Python lookNsayNth(), given below, is a generator function that generates the digits (as characters) of...
The Python lookNsayNth(), given below, is a generator function that generates the digits (as characters) of the “n-th” “look-and-say number”. E.g., 1113… generates the characters ‘1’, ‘1’, ‘1’, ‘3’, etc. Use this generator to print out the 1,000,000-th to 1,000,019-th digits of the 200th “look-and-say” number. Check: your number will start with 211121… (Keep in mind that the 1st digit has index 0 if the digits were put in an array). Note that the 200th “look-and-say” number has somewhere around...
Write a program in date_generator.py that generates all the dates of next year in mmm dd,...
Write a program in date_generator.py that generates all the dates of next year in mmm dd, yyyy format, as shown below. You need not calculate whether 2021 is a leap year--it is NOT. If you want to see a hint, scroll WAY down! Jan 1, 2021 Jan 2, 2021 Jan 3, 2021 . . . Dec 30, 2021 Dec 31, 2021 ***my teachers hint:My solution to this program is quite short, but the code is not super simple. I used...
Write a program that randomly generates 100 dates and store them into a vector. Use the...
Write a program that randomly generates 100 dates and store them into a vector. Use the Date.h provided . The dates generated must be within 1000 days after 1/1/2000. and Sort the 100 dates generated in ascending order. date.h #ifndef DATE_H_ #define DATE_H_ #include #include using namespace std; class Date {    friend ostream &operator<<( ostream &, const Date & ); private:    int day;    int month;    int year; static const int days[]; // array of days per...
python coding Suppose a list of positive numbers is given like the following list (remember this...
python coding Suppose a list of positive numbers is given like the following list (remember this is only an example and the list could be any list of positive numbers) exampleList: 15 19 10 11 8 7 3 3 1 We would like to know the “prime visibility” of each index of the list. The “prime visibility” of a given index shows how many numbers in the list with indexes lower than the given index are prime. For instance, in...
In Python, Given a list of numbers, return a list where all adjacent == elements have...
In Python, Given a list of numbers, return a list where all adjacent == elements have been reduced to a single element, so [1,2,2,3,3,2,2,4] returns [1,2,3,2,4]. You may create a new list or modify the passed in list (set function does not work in this case).
#Write a program in Python that given a list of positive integers, return another list where...
#Write a program in Python that given a list of positive integers, return another list where each element corresponds to the sum of the digits of the elements o#f the given list. #Example: # input # alist=[124, 5, 914, 21, 3421] # output # sum=[7, 5, 14, 3, 18]
Python program please no def, main, functions Given a list of negative integers, write a Python...
Python program please no def, main, functions Given a list of negative integers, write a Python program to display each integer in the list that is evenly divisible by either 5 or 7. Also, print how many of those integers were found. Sample input/output: Enter a negative integer (0 or positive to end): 5 Number of integers evenly divisible by either 5 or 7: 0 Sample input/output: Enter a negative integer (0 or positive to end): -5 -5 is evenly...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT