Find number of times every day occurs in a Year in Python

When working with calendar calculations, you might need to find how many times each day of the week occurs in a given month. This is useful for scheduling, payroll calculations, or planning recurring events.

Understanding the Problem

In any month, most days occur either 4 or 5 times. For a month with n days starting on a specific day, we need to calculate the frequency of each weekday.

Algorithm Approach

The solution works by:

  • Starting with a base count of 4 for each day (since 4 × 7 = 28 days)

  • Adding the extra days (n - 28) starting from the first day of the month

  • Incrementing the count for days that get the extra occurrences

Complete Implementation

def count_weekdays_in_month(num_days, first_day):
    weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    
    # Initialize count to 4 for each day (4 complete weeks = 28 days)
    day_count = [4 for i in range(7)]
    
    # Find the position of the first day
    start_position = -1
    for i in range(7):
        if first_day == weekdays[i]:
            start_position = i
            break
    
    # Calculate extra days beyond 28
    extra_days = num_days - 28
    
    # Distribute extra days starting from the first day
    for i in range(start_position, start_position + extra_days):
        if i > 6:
            day_count[i % 7] = 5
        else:
            day_count[i] = 5
    
    # Display results
    for i in range(7):
        print(weekdays[i], ":", day_count[i])

# Example: January has 31 days and starts on Thursday
num_days = 31
first_day = "Thursday"
count_weekdays_in_month(num_days, first_day)
Monday : 4
Tuesday : 4
Wednesday : 4
Thursday : 5
Friday : 5
Saturday : 5
Sunday : 4

How It Works

For a 31-day month starting on Thursday:

  • Base weeks: 4 complete weeks (28 days) give each day 4 occurrences

  • Extra days: 31 - 28 = 3 remaining days

  • Distribution: These 3 extra days go to Thursday, Friday, and Saturday

  • Final count: Thursday, Friday, Saturday get 5 occurrences; others get 4

Testing with Different Months

def count_weekdays_in_month(num_days, first_day):
    weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    day_count = [4 for i in range(7)]
    
    start_position = weekdays.index(first_day)
    extra_days = num_days - 28
    
    for i in range(start_position, start_position + extra_days):
        day_count[i % 7] = 5
    
    return dict(zip(weekdays, day_count))

# Test different scenarios
print("February (28 days, starts Monday):")
result1 = count_weekdays_in_month(28, "Monday")
for day, count in result1.items():
    print(f"{day}: {count}")

print("\nApril (30 days, starts Saturday):")
result2 = count_weekdays_in_month(30, "Saturday")
for day, count in result2.items():
    print(f"{day}: {count}")
February (28 days, starts Monday):
Monday: 4
Tuesday: 4
Wednesday: 4
Thursday: 4
Friday: 4
Saturday: 4
Sunday: 4

April (30 days, starts Saturday):
Monday: 4
Tuesday: 4
Wednesday: 4
Thursday: 4
Friday: 4
Saturday: 5
Sunday: 5

Conclusion

This algorithm efficiently calculates weekday frequencies by starting with a base count of 4 and distributing extra days sequentially. It's particularly useful for calendar applications and scheduling systems where you need to know the exact count of specific weekdays in a month.

---
Updated on: 2026-03-25T18:52:07+05:30

717 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements