-
Notifications
You must be signed in to change notification settings - Fork 37
Rendu Baptiste Terral #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,23 @@ | ||
| from doomsday.algorithm import get_weekday_for_date | ||
| from tests.test_algorithm import TestAlgorithm | ||
| from tests.test_date import TestDate | ||
|
|
||
|
|
||
| def main() -> None: | ||
| print("Hello world") | ||
| dates = ["2000-01-01", | ||
| "2001-01-29", | ||
| "2017-12-15", | ||
| "2023-11-15", | ||
| "2111-12-12"] # Saturday, Monday, Friday, Wednesday, Saturday | ||
| for date in dates: | ||
| print("{0} is a {1}.".format(date, get_weekday_for_date(date))) | ||
|
|
||
| test_date = TestDate() | ||
| test_date.test_valid_dates() | ||
| test_date.test_invalid_dates() | ||
|
|
||
| test_algorithm = TestAlgorithm() | ||
| test_algorithm.test_algorithm() | ||
|
|
||
|
|
||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,49 @@ | ||
| from doomsday.date import is_valid_date | ||
|
|
||
|
|
||
| def get_weekday_for_date(date: str) -> str: | ||
| return "Sunday" | ||
| if not is_valid_date(date): | ||
| return "Invalid date" | ||
|
|
||
| year, month, day = map(int, date.split('-')) | ||
| day_of_week = calculate_doomsday(year, month, day) | ||
|
|
||
| week_days = ["Sunday", "Monday", | ||
| "Tuesday", "Wednesday", | ||
| "Thursday", "Friday", "Saturday"] | ||
|
Comment on lines
+11
to
+13
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Utiliser une constante en haut du fichier, et plutôt un tuple, structure immutable |
||
| return week_days[day_of_week] | ||
|
|
||
|
|
||
| def get_year_anchor(year: int) -> int: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Vu ensemble, clarifier si possible |
||
| # Define the anchor day for the century | ||
| anchor_day = 2 | ||
|
|
||
| # Calculate the century anchor day | ||
| century = year // 100 | ||
| century_anchor = (5 * (century % 4) + anchor_day) % 7 | ||
|
|
||
| # Calculate the year anchor day | ||
| year_within_century = year % 100 | ||
| year_anchor = (century_anchor + (year_within_century // 12) | ||
| + (year_within_century % 12) | ||
| + ((year_within_century % 12) // 4) | ||
| ) % 7 | ||
|
|
||
| return year_anchor | ||
|
|
||
|
|
||
| def calculate_doomsday(year: int, month: int, day: int) -> int: | ||
| year_anchor = get_year_anchor(year) | ||
|
|
||
| # Doomsday for each month | ||
| doomsday_month = [3, 0, 0, 4, 9, 6, 11, 8, 5, 10, 7, 12] | ||
|
|
||
| # Adjustments for January and February in leap years | ||
| if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): | ||
| doomsday_month[0] = 4 | ||
| doomsday_month[1] = 1 | ||
|
|
||
| # Calculate the day of the week for the given date | ||
| day_of_week = (day - doomsday_month[month - 1] + year_anchor) % 7 | ||
|
|
||
| return day_of_week | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,22 @@ | ||
| def is_valid_date(date: str) -> bool: | ||
| return True | ||
| try: | ||
| year, month, day = map(int, date.split('-')) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bien |
||
|
|
||
| if year < 1583: | ||
| return False | ||
|
|
||
| if 1 <= month <= 12 and 1 <= day <= 31: | ||
| if month in [4, 6, 9, 11] and day > 30: | ||
| return False | ||
| elif month == 2: # February | ||
| if day > 29: | ||
| return False | ||
| elif day == 29 and not (year % 4 == 0 and | ||
| (year % 100 != 0 or year % 400 == 0)): | ||
| return False # February 29 is only valid in leap years | ||
|
Comment on lines
+8
to
+16
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trouver comment applatir un peu cette structure très imbriquée ? |
||
|
|
||
| return True | ||
| except ValueError: | ||
| pass | ||
|
|
||
| return False | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rappellons que l'algo doit expliquer à l'utilisateur quelle est son erreur de saisie (pas dans le détail, mais au mois si c'est un problème de format ou de date non existante). Probablement à l'aide de print |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Attention, le fichier main doit demander la date à l'utilisasteur qui lance le script (voir readme).
Il ne doit pas appeler les tests, qui sont appelés par la commande uniittest (voir readme aussi).