Given a list of dates, the task is to write a Python program to check if all the dates are consecutive in the list.
Input : [datetime(2019, 12, 30), datetime(2019, 12, 31), datetime(2020, 1, 1), datetime(2020, 1, 2), datetime(2020, 1, 3), datetime(2020, 1, 4)]
Output : True
Explanation : All dates are consecutive, from 30 Dec 2019 to 4 January 2020.
Input : [datetime(2019, 12, 29), datetime(2019, 12, 31), datetime(2020, 1, 1), datetime(2020, 1, 2), datetime(2020, 1, 3), datetime(2020, 1, 4)]
Output : False
Explanation : Non consecutive dates.
Method #1 : Using days() + loop
In this, we check consecutive dates by checking days difference from the previous date using days(). The iteration of all dates is done using a loop.
Python3
# Python3 code to demonstrate working of # Test if dates are consecutive # Using days() + loop from datetime import datetime, timedelta # initializing list test_list = [datetime( 2019 , 12 , 30 ), datetime( 2019 , 12 , 31 ), datetime( 2020 , 1 , 1 ), datetime( 2020 , 1 , 2 ), datetime( 2020 , 1 , 3 ), datetime( 2020 , 1 , 4 )] # printing original list print ( "The original list is : " + str (test_list)) # using loop for iterating all elements res = True for idx in range ( 1 , len (test_list)): # checking for 1 day time difference if (test_list[idx] - test_list[idx - 1 ]).days ! = 1 : res = False break # printing result print ( "Are dates consecutive : " + str (res)) |
Output:
The original list is : [datetime.datetime(2019, 12, 30, 0, 0), datetime.datetime(2019, 12, 31, 0, 0), datetime.datetime(2020, 1, 1, 0, 0), datetime.datetime(2020, 1, 2, 0, 0), datetime.datetime(2020, 1, 3, 0, 0), datetime.datetime(2020, 1, 4, 0, 0)]
Are dates consecutive : True
Time Complexity : O(n)
Space Complexity : O(1)
Method #2 : Using all() + days()
Similar to the above method, the only difference here is all() is used to check for each day consecution for a more compact solution.
Python3
# Python3 code to demonstrate working of # Test if dates are consecutive # Using all() + days() from datetime import datetime, timedelta # initializing list test_list = [datetime( 2019 , 12 , 30 ), datetime( 2019 , 12 , 31 ), datetime( 2020 , 1 , 1 ), datetime( 2020 , 1 , 2 ), datetime( 2020 , 1 , 3 ), datetime( 2020 , 1 , 4 )] # printing original list print ( "The original list is : " + str (test_list)) # using loop for iterating all elements res = all ((test_list[idx] - test_list[idx - 1 ]).days = = 1 for idx in range ( 1 , len (test_list))) # printing result print ( "Are dates consecutive : " + str (res)) |
Output:
The original list is : [datetime.datetime(2019, 12, 30, 0, 0), datetime.datetime(2019, 12, 31, 0, 0), datetime.datetime(2020, 1, 1, 0, 0), datetime.datetime(2020, 1, 2, 0, 0), datetime.datetime(2020, 1, 3, 0, 0), datetime.datetime(2020, 1, 4, 0, 0)]
Are dates consecutive : True
Time Complexity : O(n)
Space Complexity : O(1)
Method #3: Using set()
- Initialize a new list named ‘date_range‘ containing all dates from the first date in the ‘test_list‘ to the last date in the ‘test_list‘. To do this, use the date range function and add the time delta of one day to each date.
- Check if ‘date_range‘ and ‘test_list‘ contain the same dates by converting both lists to sets and comparing them. If they are the same, then the dates in ‘test_list‘ are consecutive.
- Return True if the dates are consecutive and False otherwise.
Python3
from datetime import datetime, timedelta # initializing list test_list = [datetime( 2019 , 12 , 30 ), datetime( 2019 , 12 , 31 ), datetime( 2020 , 1 , 1 ), datetime( 2020 , 1 , 2 ), datetime( 2020 , 1 , 3 ), datetime( 2020 , 1 , 4 )] # printing original list print ( "The original list is : " + str (test_list)) # using set() to check for consecutive dates date_range = [test_list[ 0 ] + timedelta(days = x) for x in range ((test_list[ - 1 ] - test_list[ 0 ]).days + 1 )] res = set (test_list) = = set (date_range) # printing result print ( "Are dates consecutive : " + str (res)) |
The original list is : [datetime.datetime(2019, 12, 30, 0, 0), datetime.datetime(2019, 12, 31, 0, 0), datetime.datetime(2020, 1, 1, 0, 0), datetime.datetime(2020, 1, 2, 0, 0), datetime.datetime(2020, 1, 3, 0, 0), datetime.datetime(2020, 1, 4, 0, 0)] Are dates consecutive : True
Time Complexity: O(n) – linear time complexity since we loop through the test_list and the date_range only once
Auxiliary Space: O(n) – linear space complexity since we create a new list, ‘date_range’, containing all dates in the given range