-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add doomsday algorithm #2903
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
Merged
Merged
Add doomsday algorithm #2903
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8255072
Added doomsday algorithm
sharpblade4 ca64cd0
Adding unit-tests to doomsday algorithm
sharpblade4 f430e3c
running black on doomsday
sharpblade4 2297ca5
adding doctest and fixing a black issue [doomsday]
sharpblade4 1b4c74c
Update doomsday.py
sharpblade4 5157c0d
fixing black issue [doomsday]
sharpblade4 7745014
Update other/doomsday.py
poyea 7a57d76
Update doomsday.py
poyea 4c327e8
adding more doctests (following review comment) [doomsday]
sharpblade4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/bin/python3 | ||
# Doomsday algorithm info: https://en.wikipedia.org/wiki/Doomsday_rule | ||
|
||
_doomsday_leap = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] | ||
_doomsday_not_leap = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] | ||
_week_day_names = { | ||
0: "Sunday", | ||
1: "Monday", | ||
2: "Tuesday", | ||
3: "Wednesday", | ||
4: "Thursday", | ||
5: "Friday", | ||
6: "Saturday", | ||
} | ||
|
||
|
||
def get_week_day(year: int, month: int, day: int) -> str: | ||
"""Returns the week-day name out of a given date. | ||
|
||
>>> get_week_day(2020, 10, 24) | ||
'Saturday' | ||
>>> get_week_day(2017, 10, 24) | ||
'Tuesday' | ||
|
||
sharpblade4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
# minimal input check: | ||
assert len(str(year)) > 2, "year should be in YYYY format" | ||
assert 1 <= month <= 12, "month should be between 1 to 12" | ||
assert 1 <= day <= 31, "day should be between 1 to 31" | ||
|
||
# Doomsday algorithm: | ||
century = year // 100 | ||
century_anchor = (5 * (century % 4) + 2) % 7 | ||
centurian = year % 100 | ||
centurian_m = centurian % 12 | ||
dooms_day = ( | ||
(centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor | ||
) % 7 | ||
day_anchor = ( | ||
_doomsday_not_leap[month - 1] | ||
if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) | ||
else _doomsday_leap[month - 1] | ||
) | ||
week_day = (dooms_day + day - day_anchor) % 7 | ||
return _week_day_names[week_day] | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.