Given a date, our task is to get the weekday index and year range, and compute the total occurrence of the date on a particular weekday in the year range.
Example:
Input : date = 13, weekdy = 5, strt, end = 1950, 2020
Output : 120
Explanation : Total dates with Friday 13 from 1950 – 2020 is 120.
Input : date = 13, weekdy = 1, strt, end = 1950, 2050
Output : 173
Explanation : Total dates with Monday, 13 from 1950 – 2050 is 173.
Method 1 : Using loop + weekday()
In this, we run a nested loop from start year to end the year and within each year, each month is checked with a date to be exact weekday as required, if found, the counter is incremented.
Python3
from datetime import datetime
date = 13
weekdy = 5
strt, end = 1950 , 2020
print ( "The date, weekday : " + str (date) + " " + str (weekdy))
res = 0
for year in range (strt, end + 1 ):
for month in range ( 1 , 13 ):
if datetime(year, month, date).weekday() = = weekdy:
res + = 1
print ( "Total dates with same weekday : " + str (res))
|
OutputThe date, weekday : 13 5
Total dates with same weekday : 120
Method 2 : Using sum() + generator expression
Similar to the above method, the only difference is generator expression is used for the task of a nested loop, sum() performs the task of summing the correct found year, month combination.
Python3
from datetime import datetime
date = 13
weekdy = 5
strt, end = 1950 , 2020
print ( "The date, weekday : " + str (date) + " " + str (weekdy))
res = sum (datetime(year, month, date).weekday() = = weekdy
for year in range (strt, end + 1 ) for month in range ( 1 , 13 ))
print ( "Total dates with same weekday : " + str (res))
|
OutputThe date, weekday : 13 5
Total dates with same weekday : 120
Method 3: Using calendar module
We can also use the calendar module in Python to iterate through all the dates in the given range of years and check if the weekday of each date is equal to the given weekday. If the weekday matches, we check if the day of the date is 13. If it is, we increment a counter variable.
Algorithm:
- Define a function named count_dates that takes four arguments – date, weekday, start, and end.
- In the function, initialize a counter variable named count to 0.
- Use a nested for loop to iterate through all the months and years in the given range.
- Use the calendar.monthcalendar() function to get a matrix representing the calendar month.
- Check if the date falls on the given weekday in the first or second week of the month.
- If the date falls on a given weekday, increment the count variable.
- Return the count variable.
Example:
Python3
import calendar
def count_dates(date, weekday, start, end):
count = 0
for year in range (start, end + 1 ):
for month in range ( 1 , 13 ):
cal = calendar.monthcalendar(year, month)
if cal[ 0 ][weekday] = = date or cal[ 1 ][weekday] = = date:
count + = 1
return count
date = 13
weekday = 5
start, end = 1950 , 2020
print (count_dates(date, weekday, start, end))
|
Time complexity: O(n), where n is the number of days in the given range of years.
Auxiliary Space: O(1)
Method 4: use a list comprehension with a filter condition to generate a list of dates with the specified weekday in the given range of years.
Steps:
- Import the datetime module.
- Initialize the date and weekday values.
- Initialize the range of years.
- Use a list comprehension to generate a list of dates with the specified weekday in the given range of years. This can be done by iterating over the years and months, and checking if the weekday of the given date matches the specified weekday. The resulting list will contain all the dates with the specified weekday in the given range of years.
- Get the length of the resulting list to obtain the final result.
- Print the result.
Python3
from datetime import datetime
date = 13
weekdy = 5
strt, end = 1950 , 2020
print ( "The date, weekday : " + str (date) + " " + str (weekdy))
dates = [datetime(year, month, date) for year in range (strt, end + 1 )
for month in range ( 1 , 13 ) if datetime(year, month, date).weekday() = = weekdy]
res = len (dates)
print ( "Total dates with same weekday : " + str (res))
|
OutputThe date, weekday : 13 5
Total dates with same weekday : 120
Time complexity: O(n) where n is the number of dates in the given range of years with the specified weekday.
Auxiliary pace: O(n) as we are storing all the dates with the specified weekday in a list.