Open In App

# Python Program to Count date on a particular weekday in given range of Years

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

 `# Python3 code to demonstrate working of``# Count date on weekday in Year Range``# Using loop + weekday()``from` `datetime ``import` `datetime` `# initializing date``date ``=` `13` `# initializing weekday``weekdy ``=` `5` `# initializing range of Years``strt, end ``=` `1950``, ``2020` `# printing Number``print``(``"The date, weekday : "` `+` `str``(date) ``+` `" "` `+` `str``(weekdy))` `res ``=` `0``for` `year ``in` `range``(strt, end ``+` `1``):` `    ``# checking each month for same date``    ``# weekday combination``    ``for` `month ``in` `range``(``1``, ``13``):``        ``if` `datetime(year, month, date).weekday() ``=``=` `weekdy:``            ``res ``+``=` `1` `# printing result``print``(``"Total dates with same weekday : "` `+` `str``(res))`

Output

```The 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

 `# Python3 code to demonstrate working of``# Count date on weekday in Year Range``# Using sum() + generator expression``from` `datetime ``import` `datetime` `# Initializing date``date ``=` `13` `# Initializing weekday``weekdy ``=` `5` `# Initializing range of Years``strt, end ``=` `1950``, ``2020` `# Printing Number``print``(``"The date, weekday : "` `+` `str``(date) ``+` `" "` `+` `str``(weekdy))` `# sum performs accumulation``res ``=` `sum``(datetime(year, month, date).weekday() ``=``=` `weekdy``          ``for` `year ``in` `range``(strt, end ``+` `1``) ``for` `month ``in` `range``(``1``, ``13``))` `# Printing result``print``(``"Total dates with same weekday : "` `+` `str``(res))`

Output

```The 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:

1. Define a function named count_dates that takes four arguments – date, weekday, start, and end.
2. In the function, initialize a counter variable named count to 0.
3. Use a nested for loop to iterate through all the months and years in the given range.
4. Use the calendar.monthcalendar() function to get a matrix representing the calendar month.
5. Check if the date falls on the given weekday in the first or second week of the month.
6. If the date falls on a given weekday, increment the count variable.
7. 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))`

Output

`120`

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:

1. Import the datetime module.
2. Initialize the date and weekday values.
3. Initialize the range of years.
4. 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.
5. Get the length of the resulting list to obtain the final result.
6. Print the result.

## Python3

 `from` `datetime ``import` `datetime` `# Initializing date``date ``=` `13` `# Initializing weekday``weekdy ``=` `5` `# Initializing range of Years``strt, end ``=` `1950``, ``2020` `# Printing Number``print``(``"The date, weekday : "` `+` `str``(date) ``+` `" "` `+` `str``(weekdy))` `# Generating list of dates with the specified weekday``# using list comprehension``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]` `# Getting the length of the list to``# obtain the final result``res ``=` `len``(dates)` `# Printing the result``print``(``"Total dates with same weekday : "` `+` `str``(res))`

Output

```The 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.