Last active
April 12, 2024 09:41
-
-
Save Donnie/94c1befde02bae86f49401ebb73c35ec to your computer and use it in GitHub Desktop.
A script to calculate the distribution of weekdays for each date (1st to 31st) for every month in 2024. Find out how often each date falls on a Saturday or Sunday. This is to avoid Revolut extra charges for weekends.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import calendar | |
def count_weekends(year): | |
# Dictionary to hold the count of Saturdays and Sundays for each date | |
weekend_counts = {date: 0 for date in range(1, 32)} | |
# Process each month and each date in the month | |
for month in range(1, 13): | |
# Get the number of days in the month | |
num_days = calendar.monthrange(year, month)[1] | |
for day in range(1, num_days + 1): | |
# Get the weekday (0=Monday, 6=Sunday) | |
weekday = calendar.weekday(year, month, day) | |
# If it's Saturday (5) or Sunday (6), increment the count | |
if weekday == 5 or weekday == 6: | |
weekend_counts[day] += 1 | |
# Print the results | |
for date in sorted(weekend_counts): | |
print(f"Date {date}: {weekend_counts[date]} weekends") | |
# Find the date with the minimum weekends | |
min_weekends = min(weekend_counts.values()) | |
least_likely_dates = [date for date, count in weekend_counts.items() if count == min_weekends] | |
print(f"\nDates that are least likely to be a weekend in {year}: {least_likely_dates}") | |
# Year to analyze | |
year = 2024 | |
count_weekends(year) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://codehs.com/sandbox/id/python-3-pEvSi5 working sandbox