From e95397e040295ef96f41cb38958ab8db70bdffe8 Mon Sep 17 00:00:00 2001 From: Kayyissa Date: Wed, 15 Nov 2023 15:12:41 +0100 Subject: [PATCH 1/6] debut input date --- __main__.py | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/__main__.py b/__main__.py index 53c2c43..7a5dc4c 100644 --- a/__main__.py +++ b/__main__.py @@ -1,5 +1,49 @@ def main() -> None: - print("Hello world") + date: str = ask_for_valid_date() +def ask_for_valid_date() -> str: + """Ask for a date until a valid one is given""" + + given_date: str = "" + + while True: + print("De quelle date souhaitez vous connaître le jour ?") + given_date = input() + if is_valid_date(given_date) is True: + return given_date + else: + print("La date doit être donnée au format YYYY-MM-dd " + "à compter de 1583") + + +def is_valid_date(date: str) -> bool: + """Check if a date is valid""" + + splited_date: list[str] = date.split("-") + + #Verify if there is 3 parts on the date + if len(splited_date) != 3: + return False + + #Verify they are numbers + for part in splited_date: + if part.isdigit() is False: + return False + + #Verify if year and month exist in our calendar + if int(splited_date[0]) >= 1583 is False: + return False + + if 1 > int(splited_date[1]) > 12: + return False + + if (1 > int(splited_date[2]) > days_in_month(int(splited_date[0]), int(splited_date[1]))): + return False + + + +def days_in_month(year: int, month: int) -> int: + return 28 + main() From 50e293b0e4c94351c28b86592bef73a457ad0b7b Mon Sep 17 00:00:00 2001 From: Kayyissa Date: Wed, 15 Nov 2023 16:53:17 +0100 Subject: [PATCH 2/6] end of valid date (hopefully) --- __main__.py | 39 +++++----------------------------- doomsday/algorithm.py | 1 + doomsday/date.py | 49 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 34 deletions(-) diff --git a/__main__.py b/__main__.py index 7a5dc4c..b93d0e3 100644 --- a/__main__.py +++ b/__main__.py @@ -1,5 +1,9 @@ +from doomsday.date import is_valid_date +from doomsday.algorithm import get_weekday_for_date + def main() -> None: date: str = ask_for_valid_date() + get_weekday_for_date(date) def ask_for_valid_date() -> str: @@ -8,42 +12,9 @@ def ask_for_valid_date() -> str: given_date: str = "" while True: - print("De quelle date souhaitez vous connaître le jour ?") + print("De quelle date souhaitez vous connaître le jour ? (YYYY-MM-dd)") given_date = input() if is_valid_date(given_date) is True: return given_date - else: - print("La date doit être donnée au format YYYY-MM-dd " - "à compter de 1583") - - -def is_valid_date(date: str) -> bool: - """Check if a date is valid""" - - splited_date: list[str] = date.split("-") - - #Verify if there is 3 parts on the date - if len(splited_date) != 3: - return False - - #Verify they are numbers - for part in splited_date: - if part.isdigit() is False: - return False - - #Verify if year and month exist in our calendar - if int(splited_date[0]) >= 1583 is False: - return False - - if 1 > int(splited_date[1]) > 12: - return False - - if (1 > int(splited_date[2]) > days_in_month(int(splited_date[0]), int(splited_date[1]))): - return False - - - -def days_in_month(year: int, month: int) -> int: - return 28 main() diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index cf81d60..ac33594 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,2 +1,3 @@ def get_weekday_for_date(date: str) -> str: + return "Sunday" diff --git a/doomsday/date.py b/doomsday/date.py index 0f8e737..b104541 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -1,2 +1,51 @@ def is_valid_date(date: str) -> bool: + """Check if a date is valid""" + + splited_date: list[str] = date.split("-") + + #Verify there is 3 parts on the date + if len(splited_date) != 3: + print("La date doit être au format YYYY-MM-dd") + return False + + #Verify they are numbers + for part in splited_date: + if part.isdigit() is False: + print("La date ne peut contenir que des nombres") + return False + + #Verify if year and month exist in our calendar + if int(splited_date[0]) < 1583: + print("Les dates ne sont prises en compte qu'à partir de 1583") + return False + + if (1 > int(splited_date[1])) or (int(splited_date[1]) > 12): + print("Les mois ne vont que de 1 à 12") + return False + + #Verify if the day is correct + days_in_this_month: int = days_in_month( + int(splited_date[0]), int(splited_date[1])) + + if (1 > int(splited_date[2])) or ( + int(splited_date[2]) > days_in_this_month): + print("Pour le mois renseigné, les jours vont de 1 à ", + days_in_this_month) + return False + return True + + +def days_in_month(year: int, month: int) -> int: + """gives the number of days in february, depending on year""" + + if month == 2: + return 29 if year % 4 == 0 and ( + year % 100 != 0 or year % 400 == 0) else 28 + + days_per_month: list[int] = [ + 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 + ] + + return days_per_month[month-1] + From 1341dcf6a7348008217af602e39568350b3fc153 Mon Sep 17 00:00:00 2001 From: Kayyissa Date: Fri, 17 Nov 2023 10:19:55 +0100 Subject: [PATCH 3/6] anchor day --- __main__.py | 15 ++++++++-- doomsday/algorithm.py | 46 +++++++++++++++++++++++++++-- doomsday/date.py | 69 +++++++++++++++++++++++-------------------- 3 files changed, 94 insertions(+), 36 deletions(-) diff --git a/__main__.py b/__main__.py index b93d0e3..f8915d0 100644 --- a/__main__.py +++ b/__main__.py @@ -2,8 +2,11 @@ from doomsday.algorithm import get_weekday_for_date def main() -> None: + """Main function asking for a date and displaying its day""" + date: str = ask_for_valid_date() - get_weekday_for_date(date) + + display_weekday_for_date(date, get_weekday_for_date(date)) def ask_for_valid_date() -> str: @@ -12,9 +15,17 @@ def ask_for_valid_date() -> str: given_date: str = "" while True: - print("De quelle date souhaitez vous connaître le jour ? (YYYY-MM-dd)") + print("Veuillez renseigner la date dont vous souhaitez " + "connaître le jour au format YYYY-MM-dd :") given_date = input() if is_valid_date(given_date) is True: return given_date + +def display_weekday_for_date(date: str, day: str) -> None: + """Display a sentence saying a date and its day""" + + print(f"Le {date} est un {day}") + + main() diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index ac33594..6ec740f 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,3 +1,45 @@ +DAYS = ( + "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche" +) + +MULTIPLE_OF_SEVEN = ( + #The maximum value that can be used is 35 + 7, 14, 21, 28, 35 +) + +INDEX_FOR_CENTURY = ( + 2, 0, 5, 3 +) + + def get_weekday_for_date(date: str) -> str: - - return "Sunday" + """Return the name of the day corresponding to the given date""" + + year, month, day = (int(value) for value in date.split("-")) + anchor_day: int = get_anchor_day(str(year)) + + + return "pouet" + + +def get_anchor_day(year: str) -> int: + """Return the anchor day for the given year""" + + #Get the last two numbers in the year + anchor_day: int = int(year[-2:len(year)]) + century: int = int(year[0:-2]) + + for i in range(2): + if anchor_day % 2 != 0: + anchor_day += 11 + anchor_day //= 2 + + #Find the lowest multiple of seven greater than the value we have, + # and keep the difference between this multiple and our value + for multiple in MULTIPLE_OF_SEVEN: + if multiple - 7 < anchor_day < multiple: + anchor_day = multiple - anchor_day + + anchor_day += INDEX_FOR_CENTURY[century % 4] + + return anchor_day diff --git a/doomsday/date.py b/doomsday/date.py index b104541..d5394a6 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -1,51 +1,56 @@ +DAYS_PER_MONTH = ( + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 +) + def is_valid_date(date: str) -> bool: - """Check if a date is valid""" + """Check if a date is valid. Which means beign 3 numbers separated by "-", + and that the year, month and day given exist in the Gregorian calendar""" - splited_date: list[str] = date.split("-") - - #Verify there is 3 parts on the date - if len(splited_date) != 3: - print("La date doit être au format YYYY-MM-dd") + if is_valid_format_date(date) is False: return False - - #Verify they are numbers - for part in splited_date: - if part.isdigit() is False: - print("La date ne peut contenir que des nombres") - return False - - #Verify if year and month exist in our calendar - if int(splited_date[0]) < 1583: + + year, month, day = (int(value) for value in date.split("-")) + + if year < 1583: print("Les dates ne sont prises en compte qu'à partir de 1583") return False - - if (1 > int(splited_date[1])) or (int(splited_date[1]) > 12): + + if (1 > month) or (month > 12): print("Les mois ne vont que de 1 à 12") return False - - #Verify if the day is correct - days_in_this_month: int = days_in_month( - int(splited_date[0]), int(splited_date[1])) - - if (1 > int(splited_date[2])) or ( - int(splited_date[2]) > days_in_this_month): + + days_in_this_month: int = days_in_month(year, month) + + if (1 > day) or (day > days_in_this_month): print("Pour le mois renseigné, les jours vont de 1 à ", days_in_this_month) return False - + + return True + +def is_valid_format_date(date: str) -> bool: + """Verify if a given date is 3 numbers separated by "-" """ + + splited_date: list[str] = date.split("-") + + if len(splited_date) != 3: + print("La date doit être au format YYYY-MM-dd") + return False + + for part in splited_date: + if part.isdigit() is False: + print("La date ne peut contenir que des nombres") + return False + return True def days_in_month(year: int, month: int) -> int: - """gives the number of days in february, depending on year""" + """Return the number of days in a month, depending on year""" + #Test if a year is a leap year for february if month == 2: return 29 if year % 4 == 0 and ( year % 100 != 0 or year % 400 == 0) else 28 - - days_per_month: list[int] = [ - 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 - ] - return days_per_month[month-1] - + return DAYS_PER_MONTH[month-1] From b40c68d035b473b5da01147b90f52b30fc7b6371 Mon Sep 17 00:00:00 2001 From: Kayyissa Date: Fri, 17 Nov 2023 16:04:02 +0100 Subject: [PATCH 4/6] tada --- __main__.py | 7 ++-- doomsday/algorithm.py | 77 +++++++++++++++++++++++++++++-------------- doomsday/date.py | 16 +++++---- 3 files changed, 65 insertions(+), 35 deletions(-) diff --git a/__main__.py b/__main__.py index f8915d0..5359c36 100644 --- a/__main__.py +++ b/__main__.py @@ -1,6 +1,7 @@ from doomsday.date import is_valid_date from doomsday.algorithm import get_weekday_for_date + def main() -> None: """Main function asking for a date and displaying its day""" @@ -15,9 +16,7 @@ def ask_for_valid_date() -> str: given_date: str = "" while True: - print("Veuillez renseigner la date dont vous souhaitez " - "connaître le jour au format YYYY-MM-dd :") - given_date = input() + given_date = input("Your date (YYYY-MM-dd):\n") if is_valid_date(given_date) is True: return given_date @@ -25,7 +24,7 @@ def ask_for_valid_date() -> str: def display_weekday_for_date(date: str, day: str) -> None: """Display a sentence saying a date and its day""" - print(f"Le {date} est un {day}") + print(f"{date} is a {day}") main() diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index 6ec740f..8392b52 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,45 +1,74 @@ DAYS = ( - "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche" + "Sunday", + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday" ) -MULTIPLE_OF_SEVEN = ( - #The maximum value that can be used is 35 - 7, 14, 21, 28, 35 -) - -INDEX_FOR_CENTURY = ( - 2, 0, 5, 3 +CENTURY_ANCHOR = ( + 3, 2, 0, 5 ) def get_weekday_for_date(date: str) -> str: """Return the name of the day corresponding to the given date""" - year, month, day = (int(value) for value in date.split("-")) - anchor_day: int = get_anchor_day(str(year)) + year, month, day = (value for value in date.split("-")) + anchor_day: int = get_anchor_day(year) - - return "pouet" + return get_weekday_by_anchor_day( + int(year), int(month), int(day), anchor_day) def get_anchor_day(year: str) -> int: """Return the anchor day for the given year""" - #Get the last two numbers in the year + # Get the last two numbers in the year anchor_day: int = int(year[-2:len(year)]) - century: int = int(year[0:-2]) + century: int = int(year[0:-2]) + 1 + + if anchor_day % 2 != 0: + anchor_day += 11 - for i in range(2): - if anchor_day % 2 != 0: - anchor_day += 11 - anchor_day //= 2 + anchor_day //= 2 - #Find the lowest multiple of seven greater than the value we have, + if anchor_day % 2 != 0: + anchor_day += 11 + + # Find the lowest multiple of seven greater than the value we have, # and keep the difference between this multiple and our value - for multiple in MULTIPLE_OF_SEVEN: - if multiple - 7 < anchor_day < multiple: - anchor_day = multiple - anchor_day + anchor_day %= 7 + anchor_day -= 7 + anchor_day = -anchor_day + + anchor_day += CENTURY_ANCHOR[(century % 4)] + + return anchor_day % 7 + + +def get_weekday_by_anchor_day( + year: int, month: int, day: int, anchor_day: int) -> str: + """Return the weekday of the given MM-dd + with the anchor day for the year""" + + is_leap_year: bool = True if year % 4 == 0 and ( + year % 100 != 0 or year % 400 == 0) else False + + doomsdays = [ + 3 if is_leap_year is False else 4, + 28 if is_leap_year is False else 29, + 14, 4, 9, 6, 11, 8, 5, 10, 7, 12 + ] + + doomsday: int = doomsdays[month - 1] + + if doomsday == day: + return DAYS[anchor_day] - anchor_day += INDEX_FOR_CENTURY[century % 4] + if day > doomsday: + return DAYS[(anchor_day + (day - doomsday)) % 7] - return anchor_day + return DAYS[(anchor_day - (doomsday - day)) % 7] diff --git a/doomsday/date.py b/doomsday/date.py index d5394a6..ff5cd77 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -2,6 +2,7 @@ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ) + def is_valid_date(date: str) -> bool: """Check if a date is valid. Which means beign 3 numbers separated by "-", and that the year, month and day given exist in the Gregorian calendar""" @@ -12,34 +13,35 @@ def is_valid_date(date: str) -> bool: year, month, day = (int(value) for value in date.split("-")) if year < 1583: - print("Les dates ne sont prises en compte qu'à partir de 1583") + print("Years begin from 1583") return False if (1 > month) or (month > 12): - print("Les mois ne vont que de 1 à 12") + print("Months go from 1 to 12") return False days_in_this_month: int = days_in_month(year, month) if (1 > day) or (day > days_in_this_month): - print("Pour le mois renseigné, les jours vont de 1 à ", - days_in_this_month) + print("For this month, days go from 1 to ", + days_in_this_month) return False return True + def is_valid_format_date(date: str) -> bool: """Verify if a given date is 3 numbers separated by "-" """ splited_date: list[str] = date.split("-") if len(splited_date) != 3: - print("La date doit être au format YYYY-MM-dd") + print("Date format should be YYYY-MM-dd") return False for part in splited_date: if part.isdigit() is False: - print("La date ne peut contenir que des nombres") + print("A date must be composed with numbers") return False return True @@ -48,7 +50,7 @@ def is_valid_format_date(date: str) -> bool: def days_in_month(year: int, month: int) -> int: """Return the number of days in a month, depending on year""" - #Test if a year is a leap year for february + # Test if a year is a leap year for february if month == 2: return 29 if year % 4 == 0 and ( year % 100 != 0 or year % 400 == 0) else 28 From 5804b386b1604c2bece1853be0ff3b2af4a1cf13 Mon Sep 17 00:00:00 2001 From: Kayyissa Date: Fri, 17 Nov 2023 16:41:30 +0100 Subject: [PATCH 5/6] fixes --- __main__.py | 2 +- doomsday/algorithm.py | 9 +++++---- doomsday/date.py | 21 ++++++++++++--------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/__main__.py b/__main__.py index 5359c36..0edf0f9 100644 --- a/__main__.py +++ b/__main__.py @@ -17,7 +17,7 @@ def ask_for_valid_date() -> str: while True: given_date = input("Your date (YYYY-MM-dd):\n") - if is_valid_date(given_date) is True: + if is_valid_date(given_date): return given_date diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index 8392b52..26b7129 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,3 +1,5 @@ +from doomsday.date import is_leap_year + DAYS = ( "Sunday", "Monday", @@ -54,12 +56,11 @@ def get_weekday_by_anchor_day( """Return the weekday of the given MM-dd with the anchor day for the year""" - is_leap_year: bool = True if year % 4 == 0 and ( - year % 100 != 0 or year % 400 == 0) else False + is_leap_year_value : bool = is_leap_year(year) doomsdays = [ - 3 if is_leap_year is False else 4, - 28 if is_leap_year is False else 29, + 3 if not is_leap_year_value else 4, + 28 if not is_leap_year_value else 29, 14, 4, 9, 6, 11, 8, 5, 10, 7, 12 ] diff --git a/doomsday/date.py b/doomsday/date.py index ff5cd77..297b184 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -7,7 +7,7 @@ def is_valid_date(date: str) -> bool: """Check if a date is valid. Which means beign 3 numbers separated by "-", and that the year, month and day given exist in the Gregorian calendar""" - if is_valid_format_date(date) is False: + if not is_valid_format_date(date): return False year, month, day = (int(value) for value in date.split("-")) @@ -17,13 +17,13 @@ def is_valid_date(date: str) -> bool: return False if (1 > month) or (month > 12): - print("Months go from 1 to 12") + print("Month goes from 1 to 12") return False - days_in_this_month: int = days_in_month(year, month) + days_in_this_month: int = get_days_in_month(year, month) if (1 > day) or (day > days_in_this_month): - print("For this month, days go from 1 to ", + print("For this month, day goes from 1 to ", days_in_this_month) return False @@ -40,19 +40,22 @@ def is_valid_format_date(date: str) -> bool: return False for part in splited_date: - if part.isdigit() is False: + if not part.isdigit(): print("A date must be composed with numbers") return False return True -def days_in_month(year: int, month: int) -> int: +def get_days_in_month(year: int, month: int) -> int: """Return the number of days in a month, depending on year""" - # Test if a year is a leap year for february if month == 2: - return 29 if year % 4 == 0 and ( - year % 100 != 0 or year % 400 == 0) else 28 + return 28 if not is_leap_year(year) else 29 return DAYS_PER_MONTH[month-1] + +def is_leap_year(year: int) -> bool: + """Verify if a year is a leap year or not""" + + return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) From c366722e79df51ba23b42fc0637d4a6725bbe5ae Mon Sep 17 00:00:00 2001 From: Kayyissa Date: Sat, 25 Nov 2023 14:16:33 +0100 Subject: [PATCH 6/6] pycodestyle content --- doomsday/algorithm.py | 2 +- doomsday/date.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index 26b7129..3c952f1 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -56,7 +56,7 @@ def get_weekday_by_anchor_day( """Return the weekday of the given MM-dd with the anchor day for the year""" - is_leap_year_value : bool = is_leap_year(year) + is_leap_year_value: bool = is_leap_year(year) doomsdays = [ 3 if not is_leap_year_value else 4, diff --git a/doomsday/date.py b/doomsday/date.py index 297b184..db2a75a 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -23,8 +23,7 @@ def is_valid_date(date: str) -> bool: days_in_this_month: int = get_days_in_month(year, month) if (1 > day) or (day > days_in_this_month): - print("For this month, day goes from 1 to ", - days_in_this_month) + print("For this month, day goes from 1 to ", days_in_this_month) return False return True @@ -55,6 +54,7 @@ def get_days_in_month(year: int, month: int) -> int: return DAYS_PER_MONTH[month-1] + def is_leap_year(year: int) -> bool: """Verify if a year is a leap year or not"""