From a685061cc1ef0b751b5f601c7708c848ff0d7a3b Mon Sep 17 00:00:00 2001 From: "DESKTOP-ISVPJFT\\Mathieu" Date: Wed, 15 Nov 2023 15:26:15 +0100 Subject: [PATCH 1/8] commit 15-11-2023 --- doomsday/date.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/doomsday/date.py b/doomsday/date.py index 0f8e737..88c08e5 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -1,2 +1,89 @@ def is_valid_date(date: str) -> bool: + """ + Retourne 'vrai' si la date est valide + Format attendu : "YYYY-MM-dd" + Exeptions : - jour : 1 caractère accepté + - mois : 1 caractère accepté + - année : 4+ caractères acceptés + Date minimale : 1583-01-01 + Affiche une explication si erreur + """ + print("Tested date : " + date) + + date_in_list: list[str] = date.split("- ") + + print("test" + date_in_list[0]) + + # Vérifier que la chaîne en paramètre est de format str-str-str + if not len(date_in_list) == 3: + print("Please use format YYYY-MM-dd") + return False + # Vérifier si la date est composée de chiffres (0-9) + + if not date_in_list[0].isnumeric(): + print("Year is not numeric format") + return False + if not date_in_list[1].isnumeric(): + print("Month is not numeric format") + return False + if not date_in_list[2].isnumeric(): + print("Day is not numeric format") + return False + + year: int = int(date_in_list[0]) + month: int = int(date_in_list[1]) + day: int = int(date_in_list[2]) + + # Vérifier si le nombre de caractère est bon (voir ci-haut) + if len(date_in_list[0]) < 4: + print("Year must have 4 or more characters") + return False + if 0 > len(date_in_list[1]) > 2: + print("Month must have 1 or 2 characters") + return False + if 0 > len(date_in_list[2]) > 2: + print("Day must have 4 or more characters") + return False + + # Vérifier que la date est postérieure à 1583 + if year < 1583: + print("Year must begin after 1583") + return False + + # Vérifier si le mois existe + if 0 > month > 13: + print("Month must be between 1 (january) and 12 (december)") + return False + + # Vérifier si le jour existe + if month == 2 and is_leap_year(year) and 0 > day > 29: + print("Day must be between 1 and 29") + return False + if month == 2 and not is_leap_year(year) and 0 > day > 30: + print("Day must be between 1 and 28") + return False + if month%2 == 1 and 0 > day > 32: + print("Day must be between 1 and 32") + return False + if month%2 == 0 and 0 > day > 31: + print("Day must be between 1 and 31") + return False + return True + +def is_valid_year(date: int) -> bool: + """ Retourne 'vrai' si l'année est valide""" + return True + +def is_valid_month(date: int) -> bool: + """ Retourne 'vrai' si le mois est valide""" + return True + +def is_valid_day(date: int) -> bool: + """ Retourne 'vrai' si le jour est valide""" + return True + +def is_leap_year(year: int) -> bool: + """Renvoie 'vrai' si l'année en paramètre est bissextile""" + + return True if (year%400 == 0) or (year%4 == 0 and year%100 !=0) else False \ No newline at end of file From 70c7f4628559de727a30ef40e7e6beafc0fe7432 Mon Sep 17 00:00:00 2001 From: "DESKTOP-ISVPJFT\\Mathieu" Date: Wed, 15 Nov 2023 20:41:30 +0100 Subject: [PATCH 2/8] commit Mathieu Chicoine 15/11/23 --- doomsday/algorithm.py | 90 ++++++++++++++++++++++++++++++++++++++++++- doomsday/date.py | 38 +++++++++--------- 2 files changed, 108 insertions(+), 20 deletions(-) diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index cf81d60..c10ed4b 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,2 +1,90 @@ def get_weekday_for_date(date: str) -> str: - return "Sunday" + """à venir""" + + print(date) + + days_of_week = ( + 'Sunday', + 'Monday', + 'Tuesday', + 'Wednesday', + 'Thursday', + 'Friday', + 'Saturday' + ) + + date_in_list: list[str] = date.split("-") + + year: int = int(date_in_list[0]) + month: int = int(date_in_list[1]) + day: int = int(date_in_list[2]) + + # 1re partie : déterminer le jour "ancre" de l'année + + year_end: float = float(date_in_list[0][2:]) # La fin de l'année + century: int = int(date_in_list[0][:2]) # Le siècle + anchor_day: int = 0 # Le jour ancre + century_value: int = 0 # Le décalage lié au siècle + + if year_end % 2 != 0: + year_end += 11 + year_end /= 2 + if year_end % 2 != 0: + year_end += 11 + + multiple_of_7: int = int(year_end) + while multiple_of_7 % 7 != 0: # Obtenir le multiple de 7 supérieur ou égal + multiple_of_7 += 1 + + match (century % 4): # Obtenir le décalage du siècle + case 0: # Dates 1600, 2000, etc. + century_value = 2 + case 1: # Dates 1700, 2100, etc. + century_value = 0 + case 2: # Dates 1800, 2200, etc. + century_value = 5 + case 3: # Dates 1500, 1900, etc. + century_value = 3 + + + anchor_day += multiple_of_7 - int(year_end) + century_value + + # 2e partie : déterminer un jour particulier en fonction du jour ancre + + if is_leap_year(year) and month == 1: + return days_of_week[(day - 4 + anchor_day) % 7] + if is_leap_year(year) and month == 2: + return days_of_week[(day - 1 + anchor_day) % 7] + + match month: + case 1: + return days_of_week[(day - 3 + anchor_day) % 7] + case 2: + return days_of_week[(day + anchor_day) % 7] + case 3: + return days_of_week[(day + anchor_day) % 7] + case 4: + return days_of_week[(day - 4 + anchor_day) % 7] + case 5: + return days_of_week[(day - 2 + anchor_day) % 7] + case 6: + return days_of_week[(day - 6 + anchor_day) % 7] + case 7: + return days_of_week[(day - 4 + anchor_day) % 7] + case 8: + return days_of_week[(day - 1 + anchor_day) % 7] + case 9: + return days_of_week[(day - 5 + anchor_day) % 7] + case 10: + return days_of_week[(day - 3 + anchor_day) % 7] + case 11: + return days_of_week[(day + anchor_day) % 7] + case 12: + return days_of_week[(day - 5 + anchor_day) % 7] + case _: + return "" + +def is_leap_year(year: int) -> bool: + """Renvoie 'vrai' si l'année en paramètre est bissextile""" + + return True if (year%400 == 0) or (year%4 == 0 and year%100 !=0) else False diff --git a/doomsday/date.py b/doomsday/date.py index 88c08e5..532554e 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -10,16 +10,15 @@ def is_valid_date(date: str) -> bool: """ print("Tested date : " + date) - date_in_list: list[str] = date.split("- ") - - print("test" + date_in_list[0]) + date_in_list: list[str] = date.split("-") # Vérifier que la chaîne en paramètre est de format str-str-str if not len(date_in_list) == 3: print("Please use format YYYY-MM-dd") return False + # Vérifier si la date est composée de chiffres (0-9) - + if not date_in_list[0].isnumeric(): print("Year is not numeric format") return False @@ -29,48 +28,49 @@ def is_valid_date(date: str) -> bool: if not date_in_list[2].isnumeric(): print("Day is not numeric format") return False - + year: int = int(date_in_list[0]) month: int = int(date_in_list[1]) day: int = int(date_in_list[2]) - + # Vérifier si le nombre de caractère est bon (voir ci-haut) if len(date_in_list[0]) < 4: print("Year must have 4 or more characters") return False - if 0 > len(date_in_list[1]) > 2: + if len(date_in_list[1]) < 0 or len(date_in_list[1]) > 2: print("Month must have 1 or 2 characters") return False - if 0 > len(date_in_list[2]) > 2: - print("Day must have 4 or more characters") + if len(date_in_list[2]) < 0 or len(date_in_list[2]) > 2: + print("Day must have 1 or 2 characters") return False - - # Vérifier que la date est postérieure à 1583 + + # Vérifier que l'année est postérieure à 1583 if year < 1583: print("Year must begin after 1583") return False - + # Vérifier si le mois existe - if 0 > month > 13: + if month > 13: print("Month must be between 1 (january) and 12 (december)") return False # Vérifier si le jour existe - if month == 2 and is_leap_year(year) and 0 > day > 29: + if month == 2 and is_leap_year(year) and day > 29: print("Day must be between 1 and 29") return False - if month == 2 and not is_leap_year(year) and 0 > day > 30: + if month == 2 and not is_leap_year(year) and day > 28: print("Day must be between 1 and 28") return False - if month%2 == 1 and 0 > day > 32: + if month%2 == 1 and day > 31: print("Day must be between 1 and 32") return False - if month%2 == 0 and 0 > day > 31: + if month%2 == 0 and day > 30: print("Day must be between 1 and 31") return False + print("Valid date") return True - + def is_valid_year(date: int) -> bool: """ Retourne 'vrai' si l'année est valide""" return True @@ -86,4 +86,4 @@ def is_valid_day(date: int) -> bool: def is_leap_year(year: int) -> bool: """Renvoie 'vrai' si l'année en paramètre est bissextile""" - return True if (year%400 == 0) or (year%4 == 0 and year%100 !=0) else False \ No newline at end of file + return True if (year%400 == 0) or (year%4 == 0 and year%100 !=0) else False From b7113dee628a013aaeddf869fe922f8f25822cf1 Mon Sep 17 00:00:00 2001 From: "DESKTOP-ISVPJFT\\Mathieu" Date: Wed, 15 Nov 2023 21:17:27 +0100 Subject: [PATCH 3/8] commit Mathieu Chicoine --- doomsday/algorithm.py | 77 ++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index c10ed4b..4aabd85 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,7 +1,8 @@ def get_weekday_for_date(date: str) -> str: - """à venir""" - - print(date) + """ + Détermine le jour du jugement dernier à partir d'une date donnée + Format attendu : YYYY-MM-dd + """ days_of_week = ( 'Sunday', @@ -21,40 +22,16 @@ def get_weekday_for_date(date: str) -> str: # 1re partie : déterminer le jour "ancre" de l'année - year_end: float = float(date_in_list[0][2:]) # La fin de l'année - century: int = int(date_in_list[0][:2]) # Le siècle - anchor_day: int = 0 # Le jour ancre - century_value: int = 0 # Le décalage lié au siècle - - if year_end % 2 != 0: - year_end += 11 - year_end /= 2 - if year_end % 2 != 0: - year_end += 11 - - multiple_of_7: int = int(year_end) - while multiple_of_7 % 7 != 0: # Obtenir le multiple de 7 supérieur ou égal - multiple_of_7 += 1 - - match (century % 4): # Obtenir le décalage du siècle - case 0: # Dates 1600, 2000, etc. - century_value = 2 - case 1: # Dates 1700, 2100, etc. - century_value = 0 - case 2: # Dates 1800, 2200, etc. - century_value = 5 - case 3: # Dates 1500, 1900, etc. - century_value = 3 - - - anchor_day += multiple_of_7 - int(year_end) + century_value + anchor_day: int = get_anchor_day(year) # 2e partie : déterminer un jour particulier en fonction du jour ancre - if is_leap_year(year) and month == 1: - return days_of_week[(day - 4 + anchor_day) % 7] - if is_leap_year(year) and month == 2: - return days_of_week[(day - 1 + anchor_day) % 7] + if is_leap_year(year): + match month: + case 1: + return days_of_week[(day - 4 + anchor_day) % 7] + case 2: + return days_of_week[(day - 1 + anchor_day) % 7] match month: case 1: @@ -88,3 +65,35 @@ def is_leap_year(year: int) -> bool: """Renvoie 'vrai' si l'année en paramètre est bissextile""" return True if (year%400 == 0) or (year%4 == 0 and year%100 !=0) else False + +def get_anchor_day(year: int) -> int: + """Détermine le jour 'ancre' d'une année""" + + year_end: float = year % 100 # La fin de l'année + century: int = int(year / 100) # Le siècle + century_value: int = 0 # Le décalage lié au siècle + + # Si fin d'année pair /2, sinon +11 et /2 puis si toujours impair +11 + if year_end % 2 != 0: + year_end += 11 + year_end /= 2 + if year_end % 2 != 0: + year_end += 11 + + # Obtenir le multiple de 7 supérieur ou égal + multiple_of_7: int = int(year_end) + while multiple_of_7 % 7 != 0: + multiple_of_7 += 1 + + # Obtenir le décalage du siècle + match (century % 4): + case 0: # Dates 1600, 2000, etc. + century_value = 2 + case 1: # Dates 1700, 2100, etc. + century_value = 0 + case 2: # Dates 1800, 2200, etc. + century_value = 5 + case 3: # Dates 1500, 1900, etc. + century_value = 3 + + return multiple_of_7 - int(year_end) + century_value From 14c1e5cfadd600f4e77129cc471a1df3c6250de3 Mon Sep 17 00:00:00 2001 From: "DESKTOP-ISVPJFT\\Mathieu" Date: Fri, 17 Nov 2023 09:33:17 +0100 Subject: [PATCH 4/8] commit Mathieu Chicoine --- doomsday/date.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doomsday/date.py b/doomsday/date.py index 532554e..ad80575 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -71,15 +71,15 @@ def is_valid_date(date: str) -> bool: print("Valid date") return True -def is_valid_year(date: int) -> bool: +def is_valid_year(year: int) -> bool: """ Retourne 'vrai' si l'année est valide""" return True -def is_valid_month(date: int) -> bool: +def is_valid_month(month: int) -> bool: """ Retourne 'vrai' si le mois est valide""" return True -def is_valid_day(date: int) -> bool: +def is_valid_day(day: int) -> bool: """ Retourne 'vrai' si le jour est valide""" return True From 2f080332a5d97f2735ff4ba9455a306639ec7743 Mon Sep 17 00:00:00 2001 From: "DESKTOP-ISVPJFT\\Mathieu" Date: Fri, 17 Nov 2023 11:01:41 +0100 Subject: [PATCH 5/8] commit TP1 doomsday Mathieu Chicoine --- doomsday/algorithm.py | 7 +--- doomsday/date.py | 94 ++++++++++++++++++++++++------------------- 2 files changed, 54 insertions(+), 47 deletions(-) diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index 4aabd85..bce7c17 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,3 +1,5 @@ +from doomsday.date import is_leap_year + def get_weekday_for_date(date: str) -> str: """ Détermine le jour du jugement dernier à partir d'une date donnée @@ -61,11 +63,6 @@ def get_weekday_for_date(date: str) -> str: case _: return "" -def is_leap_year(year: int) -> bool: - """Renvoie 'vrai' si l'année en paramètre est bissextile""" - - return True if (year%400 == 0) or (year%4 == 0 and year%100 !=0) else False - def get_anchor_day(year: int) -> int: """Détermine le jour 'ancre' d'une année""" diff --git a/doomsday/date.py b/doomsday/date.py index ad80575..3391548 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -17,73 +17,83 @@ def is_valid_date(date: str) -> bool: print("Please use format YYYY-MM-dd") return False - # Vérifier si la date est composée de chiffres (0-9) + year: str = date_in_list[0] + month: str = date_in_list[1] + day: str = date_in_list[2] - if not date_in_list[0].isnumeric(): - print("Year is not numeric format") - return False - if not date_in_list[1].isnumeric(): - print("Month is not numeric format") - return False - if not date_in_list[2].isnumeric(): - print("Day is not numeric format") + # Si le l'année, le mois et le jour sont valides, retourne Vrai + if is_valid_year(year) and is_valid_month(month) and is_valid_day(day, month, year): + print("Valid date") + return True + else: return False - year: int = int(date_in_list[0]) - month: int = int(date_in_list[1]) - day: int = int(date_in_list[2]) +def is_valid_year(year: str) -> bool: + """ Retourne 'vrai' si l'année est valide""" + # Vérifier que l'année soit composée de chiffres + if not year.isnumeric(): + print("Year is not numeric format") + return False # Vérifier si le nombre de caractère est bon (voir ci-haut) - if len(date_in_list[0]) < 4: + if len(year) < 4: print("Year must have 4 or more characters") return False - if len(date_in_list[1]) < 0 or len(date_in_list[1]) > 2: - print("Month must have 1 or 2 characters") + + # Vérifier que l'année soit postérieure à 1583 + if year < "1583": + print("Year must begin after 1583") return False - if len(date_in_list[2]) < 0 or len(date_in_list[2]) > 2: - print("Day must have 1 or 2 characters") + + return True + +def is_valid_month(month: str) -> bool: + """ Retourne 'vrai' si le mois est valide""" + # Vérifier que le mois soit composé de chiffres + if not month.isnumeric(): + print("Year is not numeric format") return False - # Vérifier que l'année est postérieure à 1583 - if year < 1583: - print("Year must begin after 1583") + # Vérifier si le nombre de caractère est bon (voir ci-haut) + if len(month) < 0 or len(month) > 2: + print("Month must have 1 or 2 characters") return False # Vérifier si le mois existe - if month > 13: + if month > "12": print("Month must be between 1 (january) and 12 (december)") return False - # Vérifier si le jour existe - if month == 2 and is_leap_year(year) and day > 29: + return True + +def is_valid_day(day: str, month: str, year: str) -> bool: + """ Retourne 'vrai' si le jour est valide""" + # Vérifier que le jour soit composé de chiffres + if not day.isnumeric(): + print("Day is not numeric format") + return False + + # Vérifier si le nombre de caractère est bon (voir ci-haut) + if len(day) < 0 or len(day) > 2: + print("Day must have 1 or 2 characters") + return False + + # Vérifier que le jour existe + if int(month) == 2 and is_leap_year(int(year)) and int(day) > 29: print("Day must be between 1 and 29") return False - if month == 2 and not is_leap_year(year) and day > 28: + if int(month) == 2 and not is_leap_year(int(year)) and int(day) > 28: print("Day must be between 1 and 28") return False - if month%2 == 1 and day > 31: - print("Day must be between 1 and 32") - return False - if month%2 == 0 and day > 30: + if int(month)%2 == 1 and int(day) > 31: print("Day must be between 1 and 31") return False + if int(month)%2 == 0 and int(day) > 30: + print("Day must be between 1 and 30") + return False - print("Valid date") - return True - -def is_valid_year(year: int) -> bool: - """ Retourne 'vrai' si l'année est valide""" - return True - -def is_valid_month(month: int) -> bool: - """ Retourne 'vrai' si le mois est valide""" - return True - -def is_valid_day(day: int) -> bool: - """ Retourne 'vrai' si le jour est valide""" return True def is_leap_year(year: int) -> bool: """Renvoie 'vrai' si l'année en paramètre est bissextile""" - return True if (year%400 == 0) or (year%4 == 0 and year%100 !=0) else False From 6066b25f5e63650e2e575b117346f551b9706044 Mon Sep 17 00:00:00 2001 From: "DESKTOP-ISVPJFT\\Mathieu" Date: Fri, 17 Nov 2023 12:16:58 +0100 Subject: [PATCH 6/8] commit TP1 doomsday Mathieu Chicoine --- __main__.py | 13 ++++++++++++- doomsday/date.py | 19 +++++++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/__main__.py b/__main__.py index 53c2c43..3786c0c 100644 --- a/__main__.py +++ b/__main__.py @@ -1,5 +1,16 @@ +from doomsday.date import is_valid_date +from doomsday.algorithm import get_weekday_for_date + def main() -> None: - print("Hello world") + """Programme principal""" + print("- Doomsday algorithm -") + while True: + date: str = input("Enter a date (expected format : ) : ") + if is_valid_date(date): + print(get_weekday_for_date(date)) + try_again: bool = input("Try another date N ? (y/n) : ").lower().strip() == 'y' + if try_again is False: + break main() diff --git a/doomsday/date.py b/doomsday/date.py index 3391548..2971757 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -8,12 +8,11 @@ def is_valid_date(date: str) -> bool: Date minimale : 1583-01-01 Affiche une explication si erreur """ - print("Tested date : " + date) date_in_list: list[str] = date.split("-") # Vérifier que la chaîne en paramètre est de format str-str-str - if not len(date_in_list) == 3: + if len(date_in_list) != 3: print("Please use format YYYY-MM-dd") return False @@ -22,11 +21,7 @@ def is_valid_date(date: str) -> bool: day: str = date_in_list[2] # Si le l'année, le mois et le jour sont valides, retourne Vrai - if is_valid_year(year) and is_valid_month(month) and is_valid_day(day, month, year): - print("Valid date") - return True - else: - return False + return is_valid_year(year) and is_valid_month(month) and is_valid_day(day, month, year) def is_valid_year(year: str) -> bool: """ Retourne 'vrai' si l'année est valide""" @@ -41,7 +36,7 @@ def is_valid_year(year: str) -> bool: return False # Vérifier que l'année soit postérieure à 1583 - if year < "1583": + if int(year) < 1583: print("Year must begin after 1583") return False @@ -55,12 +50,12 @@ def is_valid_month(month: str) -> bool: return False # Vérifier si le nombre de caractère est bon (voir ci-haut) - if len(month) < 0 or len(month) > 2: + if len(month) < 1 or len(month) > 2: print("Month must have 1 or 2 characters") return False # Vérifier si le mois existe - if month > "12": + if int(month) > 12: print("Month must be between 1 (january) and 12 (december)") return False @@ -74,7 +69,7 @@ def is_valid_day(day: str, month: str, year: str) -> bool: return False # Vérifier si le nombre de caractère est bon (voir ci-haut) - if len(day) < 0 or len(day) > 2: + if len(day) < 1 or len(day) > 2: print("Day must have 1 or 2 characters") return False @@ -96,4 +91,4 @@ def is_valid_day(day: str, month: str, year: str) -> bool: def is_leap_year(year: int) -> bool: """Renvoie 'vrai' si l'année en paramètre est bissextile""" - return True if (year%400 == 0) or (year%4 == 0 and year%100 !=0) else False + return (year%400 == 0) or (year%4 == 0 and year%100 !=0) From 0468b3ba5af08ec6280d3f040eeeb7b28c056f2d Mon Sep 17 00:00:00 2001 From: "DESKTOP-ISVPJFT\\Mathieu" Date: Fri, 17 Nov 2023 15:58:46 +0100 Subject: [PATCH 7/8] commit TP1 doomsday Mathieu Chicoine --- __main__.py | 6 ++++-- doomsday/algorithm.py | 22 ++++++++++++---------- doomsday/date.py | 13 +++++++++---- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/__main__.py b/__main__.py index 3786c0c..8697d33 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: """Programme principal""" print("- Doomsday algorithm -") @@ -9,8 +10,9 @@ def main() -> None: date: str = input("Enter a date (expected format : ) : ") if is_valid_date(date): print(get_weekday_for_date(date)) - try_again: bool = input("Try another date N ? (y/n) : ").lower().strip() == 'y' - if try_again is False: + try_again: str = input("Try another date ? (y/n) : ").lower().strip() + if try_again != "y": break + main() diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index bce7c17..6528569 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,5 +1,6 @@ from doomsday.date import is_leap_year + def get_weekday_for_date(date: str) -> str: """ Détermine le jour du jugement dernier à partir d'une date donnée @@ -18,9 +19,9 @@ def get_weekday_for_date(date: str) -> str: date_in_list: list[str] = date.split("-") - year: int = int(date_in_list[0]) - month: int = int(date_in_list[1]) - day: int = int(date_in_list[2]) + year: int = int(date_in_list[0]) + month: int = int(date_in_list[1]) + day: int = int(date_in_list[2]) # 1re partie : déterminer le jour "ancre" de l'année @@ -63,12 +64,13 @@ def get_weekday_for_date(date: str) -> str: case _: return "" + def get_anchor_day(year: int) -> int: """Détermine le jour 'ancre' d'une année""" - year_end: float = year % 100 # La fin de l'année - century: int = int(year / 100) # Le siècle - century_value: int = 0 # Le décalage lié au siècle + year_end: float = year % 100 # La fin de l'année + century: int = int(year / 100) # Le siècle + century_value: int = 0 # Le décalage lié au siècle # Si fin d'année pair /2, sinon +11 et /2 puis si toujours impair +11 if year_end % 2 != 0: @@ -84,13 +86,13 @@ def get_anchor_day(year: int) -> int: # Obtenir le décalage du siècle match (century % 4): - case 0: # Dates 1600, 2000, etc. + case 0: # Dates 1600, 2000, etc. century_value = 2 - case 1: # Dates 1700, 2100, etc. + case 1: # Dates 1700, 2100, etc. century_value = 0 - case 2: # Dates 1800, 2200, etc. + case 2: # Dates 1800, 2200, etc. century_value = 5 - case 3: # Dates 1500, 1900, etc. + case 3: # Dates 1500, 1900, etc. century_value = 3 return multiple_of_7 - int(year_end) + century_value diff --git a/doomsday/date.py b/doomsday/date.py index 2971757..3bafef7 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -21,7 +21,9 @@ def is_valid_date(date: str) -> bool: day: str = date_in_list[2] # Si le l'année, le mois et le jour sont valides, retourne Vrai - return is_valid_year(year) and is_valid_month(month) and is_valid_day(day, month, year) + return is_valid_year(year) and is_valid_month(month) and \ + is_valid_day(day, month, year) + def is_valid_year(year: str) -> bool: """ Retourne 'vrai' si l'année est valide""" @@ -42,6 +44,7 @@ def is_valid_year(year: str) -> bool: return True + def is_valid_month(month: str) -> bool: """ Retourne 'vrai' si le mois est valide""" # Vérifier que le mois soit composé de chiffres @@ -61,6 +64,7 @@ def is_valid_month(month: str) -> bool: return True + def is_valid_day(day: str, month: str, year: str) -> bool: """ Retourne 'vrai' si le jour est valide""" # Vérifier que le jour soit composé de chiffres @@ -80,15 +84,16 @@ def is_valid_day(day: str, month: str, year: str) -> bool: if int(month) == 2 and not is_leap_year(int(year)) and int(day) > 28: print("Day must be between 1 and 28") return False - if int(month)%2 == 1 and int(day) > 31: + if int(month) % 2 == 1 and int(day) > 31: print("Day must be between 1 and 31") return False - if int(month)%2 == 0 and int(day) > 30: + if int(month) % 2 == 0 and int(day) > 30: print("Day must be between 1 and 30") return False return True + def is_leap_year(year: int) -> bool: """Renvoie 'vrai' si l'année en paramètre est bissextile""" - return (year%400 == 0) or (year%4 == 0 and year%100 !=0) + return (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0) From a8fafa4b8478d6afbe6c7906115b02842e9eb4da Mon Sep 17 00:00:00 2001 From: "DESKTOP-ISVPJFT\\Mathieu" Date: Mon, 20 Nov 2023 02:34:49 +0100 Subject: [PATCH 8/8] commit TP1 doomsday Mathieu Chicoine --- doomsday/algorithm.py | 83 ++++++++++++++----------------------------- doomsday/date.py | 13 +++---- 2 files changed, 30 insertions(+), 66 deletions(-) diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index 6528569..9e920d5 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,22 +1,27 @@ from doomsday.date import is_leap_year +DAYS_OF_WEEK = ( + 'Sunday', + 'Monday', + 'Tuesday', + 'Wednesday', + 'Thursday', + 'Friday', + 'Saturday' + ) + +PIVOT_DAY_LEAP = (4, 1) + +PIVOT_DAY = (3, 0, 0, 4, 2, 6, 4, 1, 5, 3, 0, 5) + + def get_weekday_for_date(date: str) -> str: """ Détermine le jour du jugement dernier à partir d'une date donnée Format attendu : YYYY-MM-dd """ - days_of_week = ( - 'Sunday', - 'Monday', - 'Tuesday', - 'Wednesday', - 'Thursday', - 'Friday', - 'Saturday' - ) - date_in_list: list[str] = date.split("-") year: int = int(date_in_list[0]) @@ -29,70 +34,34 @@ def get_weekday_for_date(date: str) -> str: # 2e partie : déterminer un jour particulier en fonction du jour ancre - if is_leap_year(year): - match month: - case 1: - return days_of_week[(day - 4 + anchor_day) % 7] - case 2: - return days_of_week[(day - 1 + anchor_day) % 7] - - match month: - case 1: - return days_of_week[(day - 3 + anchor_day) % 7] - case 2: - return days_of_week[(day + anchor_day) % 7] - case 3: - return days_of_week[(day + anchor_day) % 7] - case 4: - return days_of_week[(day - 4 + anchor_day) % 7] - case 5: - return days_of_week[(day - 2 + anchor_day) % 7] - case 6: - return days_of_week[(day - 6 + anchor_day) % 7] - case 7: - return days_of_week[(day - 4 + anchor_day) % 7] - case 8: - return days_of_week[(day - 1 + anchor_day) % 7] - case 9: - return days_of_week[(day - 5 + anchor_day) % 7] - case 10: - return days_of_week[(day - 3 + anchor_day) % 7] - case 11: - return days_of_week[(day + anchor_day) % 7] - case 12: - return days_of_week[(day - 5 + anchor_day) % 7] - case _: - return "" + if is_leap_year(year) and (month == 1 or month == 2): + return DAYS_OF_WEEK[(day - PIVOT_DAY[month - 1] + anchor_day) % 7] + else: + return DAYS_OF_WEEK[(day - PIVOT_DAY[month - 1] + anchor_day) % 7] def get_anchor_day(year: int) -> int: """Détermine le jour 'ancre' d'une année""" - year_end: float = year % 100 # La fin de l'année + year_end: int = year % 100 # La fin de l'année century: int = int(year / 100) # Le siècle century_value: int = 0 # Le décalage lié au siècle # Si fin d'année pair /2, sinon +11 et /2 puis si toujours impair +11 if year_end % 2 != 0: year_end += 11 - year_end /= 2 + year_end //= 2 if year_end % 2 != 0: year_end += 11 # Obtenir le multiple de 7 supérieur ou égal multiple_of_7: int = int(year_end) - while multiple_of_7 % 7 != 0: - multiple_of_7 += 1 + if (multiple_of_7 % 7) != 0: + multiple_of_7 += 7 - multiple_of_7 % 7 # Obtenir le décalage du siècle - match (century % 4): - case 0: # Dates 1600, 2000, etc. - century_value = 2 - case 1: # Dates 1700, 2100, etc. - century_value = 0 - case 2: # Dates 1800, 2200, etc. - century_value = 5 - case 3: # Dates 1500, 1900, etc. - century_value = 3 + century_values = [2, 0, 5, 3] + + century_value = century_values[century % 4] return multiple_of_7 - int(year_end) + century_value diff --git a/doomsday/date.py b/doomsday/date.py index 3bafef7..a8373c0 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -32,11 +32,6 @@ def is_valid_year(year: str) -> bool: print("Year is not numeric format") return False - # Vérifier si le nombre de caractère est bon (voir ci-haut) - if len(year) < 4: - print("Year must have 4 or more characters") - return False - # Vérifier que l'année soit postérieure à 1583 if int(year) < 1583: print("Year must begin after 1583") @@ -53,12 +48,12 @@ def is_valid_month(month: str) -> bool: return False # Vérifier si le nombre de caractère est bon (voir ci-haut) - if len(month) < 1 or len(month) > 2: - print("Month must have 1 or 2 characters") - return False + # if len(month) < 1 or len(month) > 2: + # print("Month must have 1 or 2 characters") + # return False # Vérifier si le mois existe - if int(month) > 12: + if int(month) < 1 and int(month) > 12: print("Month must be between 1 (january) and 12 (december)") return False