Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: day_occurrence_calculator.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script calculates the occurrences of a specified day falling on each day of the week within a given time period.
- Requirements:
- - Python 3.x
- Functions:
- - main():
- - Prompts the user to enter start and end years, start and end months, and a day of the month.
- - Calculates the occurrences of the specified day falling on each day of the week within the specified period.
- - Prints the results in a formatted table.
- - get_day_of_week(year, month, day):
- - Returns the index of the day of the week for a given date.
- - 0 for Monday, 1 for Tuesday, ..., 6 for Sunday.
- - Uses Zeller's Congruence algorithm:
- h = (q + ⌊5 * 13 * (m + 1) / 5⌋ + K + ⌊K / 4⌋ + ⌊J / 4⌋ - 2 * J) mod 7
- Usage:
- - Run the script and follow the prompts to enter start and end years, start and end months, and a day of the month.
- - The script will then calculate and display the occurrences of the specified day falling on each day of the week.
- Additional Notes:
- - Ensure the input values are within valid ranges:
- - Years should be positive integers, months should be between 1 and 12,
- - Days of the month should be between 1 and 31.
- """
- def main():
- """
- Prompts the user to enter start and end years, start and end months, and a day of the month.
- Calculates the occurrences of the specified day falling on each day of the week within the specified period.
- Prints the results in a formatted table.
- """
- start_year = int(input("Enter the start year: "))
- start_month = int(input("Enter the start month (1-12): "))
- end_year = int(input("Enter the end year: "))
- end_month = int(input("Enter the end month (1-12): "))
- day_of_month = int(input("Enter the day of the month you want to count occurrences for (1-31): "))
- if (start_year < 0 or end_year < start_year or
- start_month < 1 or start_month > 12 or
- end_month < 1 or end_month > 12 or
- day_of_month < 1 or day_of_month > 31):
- print("Invalid input. Please enter valid years, valid months, and a valid day of the month.")
- return
- day_names = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
- counts = [0] * 7
- for year in range(start_year, end_year + 1):
- start = start_month if year == start_year else 1
- end = end_month + 1 if year == end_year else 13
- for month in range(start, end):
- # Check if the specified day of the month falls on the same day of the week
- day_of_week = get_day_of_week(year, month, day_of_month)
- counts[day_of_week] += 1
- print()
- print("-" * 88)
- print("{:^88}".format("Occurrences of the specified day ({}) falling on each day of the week (Monday to Sunday)".format(day_of_month)))
- print("-" * 88)
- print("{:^44}{:^44}".format("Day", "Count"))
- print("-" * 88)
- for i in range(7):
- print("{:^44}{:^44}".format(day_names[i], counts[i]))
- print("-" * 88)
- print()
- return 0
- def get_day_of_week(year, month, day):
- """
- Returns the index of the day of the week for a given date.
- 0 for Monday, 1 for Tuesday, ..., 6 for Sunday.
- Uses Zeller's Congruence algorithm.
- """
- if month < 3:
- month += 12
- year -= 1
- K = year % 100
- J = year // 100
- h = (day + ((13 * (month + 1)) // 5) + K + (K // 4) + (J // 4) - (2 * J)) % 7
- return (h + 5) % 7
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement