From 0a8004dcd74baecd100a56b2b4d1edf48280390e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFs=20L=2E?= <36632834+LifestreamVII@users.noreply.github.com> Date: Wed, 15 Nov 2023 15:36:48 +0100 Subject: [PATCH 1/3] Main file --- __main__.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/__main__.py b/__main__.py index 53c2c43..c7dd5e8 100644 --- a/__main__.py +++ b/__main__.py @@ -1,5 +1,21 @@ -def main() -> None: - print("Hello world") +import datetime +def input_date() -> None: + """Asks for a date and check if its valid""" + time_str = str(input("Enter date in this format yyyy-mm-dd \n >>")) + try: + # year = time_str[0:4] + # month = time_str[5:7] + # day = time_str[8:10] + date = time_str.split("-", 3) + year = int(date[0]) + month = int(date[1]) + day = int(date[2]) + if (year > 999 and 1 <= month <= 12 and 1 <= day <= 31): + print("Valid date") + else: + raise Exception("Invalid date") + except Exception as e: + print(e) -main() +input_date() \ No newline at end of file From c3acb5be6ae9caae6b1afbe2d6d02d4796e2f330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFs=20L=2E?= <36632834+LifestreamVII@users.noreply.github.com> Date: Wed, 15 Nov 2023 16:02:02 +0100 Subject: [PATCH 2/3] Context on errors --- __main__.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/__main__.py b/__main__.py index c7dd5e8..5a7a07f 100644 --- a/__main__.py +++ b/__main__.py @@ -1,5 +1,3 @@ -import datetime - def input_date() -> None: """Asks for a date and check if its valid""" time_str = str(input("Enter date in this format yyyy-mm-dd \n >>")) @@ -8,14 +6,17 @@ def input_date() -> None: # month = time_str[5:7] # day = time_str[8:10] date = time_str.split("-", 3) - year = int(date[0]) - month = int(date[1]) - day = int(date[2]) - if (year > 999 and 1 <= month <= 12 and 1 <= day <= 31): - print("Valid date") - else: - raise Exception("Invalid date") - except Exception as e: + year = int(date[0]) if int(date[0]) >= 1583 else None + month = int(date[1]) if 1 <= int(date[1]) <= 12 else None + day = int(date[2]) if 1 <= int(date[2]) <= 31 else None + if year is None: + raise ValueError("Invalid year") + if month is None: + raise ValueError("Invalid month") + if day is None: + raise ValueError("Invalid day") + print("Valid date") + except ValueError as e: print(e) input_date() \ No newline at end of file From 37e3624f82ece9fc0017567ad14266f1b85d44f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFs=20L?= <36632834+LifestreamVII@users.noreply.github.com> Date: Mon, 20 Nov 2023 01:47:23 +0100 Subject: [PATCH 3/3] Add files via upload --- __main__.py | 33 +++++++++++------------------ doomsday/algorithm.py | 48 +++++++++++++++++++++++++++++++++++++++++-- doomsday/date.py | 32 +++++++++++++++++++++++++++-- 3 files changed, 88 insertions(+), 25 deletions(-) diff --git a/__main__.py b/__main__.py index 5a7a07f..c1a74e5 100644 --- a/__main__.py +++ b/__main__.py @@ -1,22 +1,13 @@ -def input_date() -> None: - """Asks for a date and check if its valid""" - time_str = str(input("Enter date in this format yyyy-mm-dd \n >>")) - try: - # year = time_str[0:4] - # month = time_str[5:7] - # day = time_str[8:10] - date = time_str.split("-", 3) - year = int(date[0]) if int(date[0]) >= 1583 else None - month = int(date[1]) if 1 <= int(date[1]) <= 12 else None - day = int(date[2]) if 1 <= int(date[2]) <= 31 else None - if year is None: - raise ValueError("Invalid year") - if month is None: - raise ValueError("Invalid month") - if day is None: - raise ValueError("Invalid day") - print("Valid date") - except ValueError as e: - print(e) - +from doomsday.date import is_valid_date +from doomsday.algorithm import get_weekday_for_date + +def input_date() -> None: + """Asks for a date and check if its valid""" + time_str = str(input("Enter date in this format yyyy-mm-dd \n >>")) + if is_valid_date(time_str): + result = get_weekday_for_date(time_str) + print(f"The day of the week is: {result}") + else: + return False + input_date() \ No newline at end of file diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index cf81d60..15ceb88 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,2 +1,46 @@ -def get_weekday_for_date(date: str) -> str: - return "Sunday" +def get_anchor_day(year: int) -> int: + + # Extract the century + century = year // 100 * 100 + + # Century offset + anchor = ((century // 100) % 4) * 5 + + # Add the offset based on the century + anchor = (anchor + 2) % 7 + + return anchor + + +def get_doomsday(year): + + # Extract the last two digits of the year + year_digits = year % 100 + + # Divide by 12 and take the remainder + quotient = year_digits // 12 + remainder = year_digits - 12*quotient + + # Apply the doomsday formula + doomsday = (remainder // 4) + + doomsday = (quotient+remainder+doomsday+get_anchor_day(year)) % 7 + + return doomsday + +def get_weekday_for_date(date: str): + + year, month, day = map(int, date.split("-")) + + anchor_day = get_anchor_day(year) + + # Get Doomsday + doomsday = get_doomsday(year) + + # Calculate day_of_week_index + day_of_week_index = (day - doomsday + anchor_day) % 7 + + days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] + + # Get the corresponding day in the array + return days_of_week[day_of_week_index] diff --git a/doomsday/date.py b/doomsday/date.py index 0f8e737..bb66af8 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -1,2 +1,30 @@ -def is_valid_date(date: str) -> bool: - return True +def is_valid_date(date: str): + try: + if not date: + raise ValueError("Empty date string") + + date = date.split("-", 3) + if len(date) != 3: + raise ValueError("Invalid date format") + + year = int(date[0]) if date[0] and int(date[0]) >= 1583 else None + month = int(date[1]) if date[1] and 1 <= int(date[1]) <= 12 else None + if year is None: + raise ValueError("Invalid year") + if month is None: + raise ValueError("Invalid month") + if date[2] and month in [1, 3, 5, 7, 8, 10, 12]: + day = int(date[2]) if 1 <= int(date[2]) <= 31 else None + elif date[2] and month in [4, 6, 9, 11]: + day = int(date[2]) if 1 <= int(date[2]) <= 30 else None + elif date[2] and month == 2: + day = int(date[2]) if is_leap_year(year) and int(date[2]) > 0 and int(date[2]) < 30 or not is_leap_year(year) and int(date[2]) > 0 and int(date[2]) < 29 else None + if day is None: + raise ValueError("Invalid day") + return True + + except ValueError as e: + print(e) + +def is_leap_year(year: int) -> bool: + return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)