Advertisement
Python253

day_occurrence_calculator

May 23rd, 2024
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.68 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: day_occurrence_calculator.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script calculates the occurrences of a specified day falling on each day of the week within a given time period.
  10.  
  11. Requirements:
  12.    - Python 3.x
  13.  
  14. Functions:
  15.    - main():
  16.        - Prompts the user to enter start and end years, start and end months, and a day of the month.
  17.        - Calculates the occurrences of the specified day falling on each day of the week within the specified period.
  18.        - Prints the results in a formatted table.
  19.  
  20.    - get_day_of_week(year, month, day):
  21.        - Returns the index of the day of the week for a given date.
  22.        - 0 for Monday, 1 for Tuesday, ..., 6 for Sunday.
  23.        - Uses Zeller's Congruence algorithm:
  24.            
  25.            h = (q + ⌊5 * 13 * (m + 1) / 5⌋ + K + ⌊K / 4⌋ + ⌊J / 4⌋ - 2 * J) mod 7
  26.  
  27. Usage:
  28.    - Run the script and follow the prompts to enter start and end years, start and end months, and a day of the month.
  29.    - The script will then calculate and display the occurrences of the specified day falling on each day of the week.
  30.  
  31. Additional Notes:
  32.    - Ensure the input values are within valid ranges:
  33.    - Years should be positive integers, months should be between 1 and 12,
  34.    - Days of the month should be between 1 and 31.
  35. """
  36.  
  37. def main():
  38.     """
  39.    Prompts the user to enter start and end years, start and end months, and a day of the month.
  40.    Calculates the occurrences of the specified day falling on each day of the week within the specified period.
  41.    Prints the results in a formatted table.
  42.    """
  43.     start_year = int(input("Enter the start year: "))
  44.     start_month = int(input("Enter the start month (1-12): "))
  45.     end_year = int(input("Enter the end year: "))
  46.     end_month = int(input("Enter the end month (1-12): "))
  47.     day_of_month = int(input("Enter the day of the month you want to count occurrences for (1-31): "))
  48.    
  49.     if (start_year < 0 or end_year < start_year or
  50.             start_month < 1 or start_month > 12 or
  51.             end_month < 1 or end_month > 12 or
  52.             day_of_month < 1 or day_of_month > 31):
  53.         print("Invalid input. Please enter valid years, valid months, and a valid day of the month.")
  54.         return
  55.  
  56.     day_names = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
  57.     counts = [0] * 7
  58.  
  59.     for year in range(start_year, end_year + 1):
  60.         start = start_month if year == start_year else 1
  61.         end = end_month + 1 if year == end_year else 13
  62.         for month in range(start, end):
  63.             # Check if the specified day of the month falls on the same day of the week
  64.             day_of_week = get_day_of_week(year, month, day_of_month)
  65.             counts[day_of_week] += 1
  66.    
  67.     print()
  68.     print("-" * 88)
  69.     print("{:^88}".format("Occurrences of the specified day ({}) falling on each day of the week (Monday to Sunday)".format(day_of_month)))
  70.     print("-" * 88)
  71.     print("{:^44}{:^44}".format("Day", "Count"))
  72.     print("-" * 88)
  73.     for i in range(7):
  74.         print("{:^44}{:^44}".format(day_names[i], counts[i]))
  75.     print("-" * 88)
  76.     print()
  77.  
  78.     return 0
  79.  
  80. def get_day_of_week(year, month, day):
  81.     """
  82.    Returns the index of the day of the week for a given date.
  83.    0 for Monday, 1 for Tuesday, ..., 6 for Sunday.
  84.    Uses Zeller's Congruence algorithm.
  85.    """
  86.     if month < 3:
  87.         month += 12
  88.         year -= 1
  89.     K = year % 100
  90.     J = year // 100
  91.     h = (day + ((13 * (month + 1)) // 5) + K + (K // 4) + (J // 4) - (2 * J)) % 7
  92.     return (h + 5) % 7
  93.  
  94. if __name__ == "__main__":
  95.     main()
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement