From 8255072413db2df67b900cafff3693e5f77aa6c5 Mon Sep 17 00:00:00 2001 From: Ron U Date: Tue, 6 Oct 2020 09:08:33 +0300 Subject: [PATCH 1/9] Added doomsday algorithm --- other/doomsday.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 other/doomsday.py diff --git a/other/doomsday.py b/other/doomsday.py new file mode 100644 index 000000000000..dd0914a8cc3f --- /dev/null +++ b/other/doomsday.py @@ -0,0 +1,46 @@ +#!/bin/python3 + +""" +The doomsday algorithm was invented by John Conway and returns +the week-day (e.g. Sunday/Monday/Tuesday/etc) given +a date (e.g. 24/10/1819). + +For more 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: + # minimal input check: + assert len(str(year)) > 2, 'Please supply year in YYYY format' + assert 1 <= month <= 12, 'Invalid month value, please give a number between 1 to 12' + assert 1 <= day <= 31, 'Invalid day value, please give a number 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__': + # unit-test: + assert get_week_day(2020, 10, 24) == 'Saturday' From ca64cd09142d1e9f9abb54d4624c124b21b23917 Mon Sep 17 00:00:00 2001 From: Ron U Date: Tue, 6 Oct 2020 09:19:47 +0300 Subject: [PATCH 2/9] Adding unit-tests to doomsday algorithm --- other/doomsday.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/other/doomsday.py b/other/doomsday.py index dd0914a8cc3f..6bbaa5ced495 100644 --- a/other/doomsday.py +++ b/other/doomsday.py @@ -23,6 +23,14 @@ 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 + + """ # minimal input check: assert len(str(year)) > 2, 'Please supply year in YYYY format' assert 1 <= month <= 12, 'Invalid month value, please give a number between 1 to 12' @@ -43,4 +51,4 @@ def get_week_day(year: int, month: int, day: int) -> str: if __name__ == '__main__': # unit-test: - assert get_week_day(2020, 10, 24) == 'Saturday' + assert get_week_day(2020, 10, 24) == "Saturday" From f430e3c751b47a002f219d80083e3940aab246c4 Mon Sep 17 00:00:00 2001 From: Ron U Date: Tue, 6 Oct 2020 09:34:20 +0300 Subject: [PATCH 3/9] running black on doomsday --- other/doomsday.py | 48 +++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/other/doomsday.py b/other/doomsday.py index 6bbaa5ced495..4a93d7d72969 100644 --- a/other/doomsday.py +++ b/other/doomsday.py @@ -9,46 +9,50 @@ 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" -} +_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. - + """Returns the week-day name out of a given date. + >>> get_week_day(2020, 10, 24) Saturday >>> get_week_day(2017, 10, 24) Tuesday - + """ # minimal input check: - assert len(str(year)) > 2, 'Please supply year in YYYY format' - assert 1 <= month <= 12, 'Invalid month value, please give a number between 1 to 12' - assert 1 <= day <= 31, 'Invalid day value, please give a number between 1 to 31' + assert len(str(year)) > 2, "Please supply year in YYYY format" + assert 1 <= month <= 12, "Invalid month value, please give a number between 1 to 12" + assert 1 <= day <= 31, "Invalid day value, please give a number 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] + 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__': +if __name__ == "__main__": # unit-test: assert get_week_day(2020, 10, 24) == "Saturday" From 2297ca571dd5cef6a8183a2199ed522774adb3c0 Mon Sep 17 00:00:00 2001 From: Ron U Date: Tue, 6 Oct 2020 09:42:27 +0300 Subject: [PATCH 4/9] adding doctest and fixing a black issue [doomsday] --- other/doomsday.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/other/doomsday.py b/other/doomsday.py index 4a93d7d72969..7cb46815dd1b 100644 --- a/other/doomsday.py +++ b/other/doomsday.py @@ -1,13 +1,5 @@ #!/bin/python3 - -""" -The doomsday algorithm was invented by John Conway and returns -the week-day (e.g. Sunday/Monday/Tuesday/etc) given -a date (e.g. 24/10/1819). - -For more info: - https://en.wikipedia.org/wiki/Doomsday_rule -""" +# For more information on the doomsday algorithm please see: 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] @@ -26,9 +18,9 @@ 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 + 'Saturday' >>> get_week_day(2017, 10, 24) - Tuesday + 'Tuesday' """ # minimal input check: @@ -54,5 +46,6 @@ def get_week_day(year: int, month: int, day: int) -> str: if __name__ == "__main__": - # unit-test: - assert get_week_day(2020, 10, 24) == "Saturday" + import doctest + + doctest.testmod() From 1b4c74cfb23ac8b689e81cd4ee76f18b4e0d6929 Mon Sep 17 00:00:00 2001 From: Ron U Date: Tue, 6 Oct 2020 09:47:33 +0300 Subject: [PATCH 5/9] Update doomsday.py --- other/doomsday.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/other/doomsday.py b/other/doomsday.py index 7cb46815dd1b..ea8acc419b4e 100644 --- a/other/doomsday.py +++ b/other/doomsday.py @@ -1,5 +1,6 @@ #!/bin/python3 -# For more information on the doomsday algorithm please see: https://en.wikipedia.org/wiki/Doomsday_rule +# For more information on the doomsday algorithm +# please see: 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] @@ -24,9 +25,9 @@ def get_week_day(year: int, month: int, day: int) -> str: """ # minimal input check: - assert len(str(year)) > 2, "Please supply year in YYYY format" - assert 1 <= month <= 12, "Invalid month value, please give a number between 1 to 12" - assert 1 <= day <= 31, "Invalid day value, please give a number between 1 to 31" + 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 From 5157c0d2dc71e9a77014fd68585d8a8d17b2e7c1 Mon Sep 17 00:00:00 2001 From: Ron U Date: Tue, 6 Oct 2020 09:55:15 +0300 Subject: [PATCH 6/9] fixing black issue [doomsday] --- other/doomsday.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/other/doomsday.py b/other/doomsday.py index ea8acc419b4e..7c810d99f522 100644 --- a/other/doomsday.py +++ b/other/doomsday.py @@ -1,6 +1,5 @@ #!/bin/python3 -# For more information on the doomsday algorithm -# please see: https://en.wikipedia.org/wiki/Doomsday_rule +# 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] From 77450144b1fafcf8ebac586e6704805d85284564 Mon Sep 17 00:00:00 2001 From: John Law Date: Tue, 6 Oct 2020 23:02:56 +0800 Subject: [PATCH 7/9] Update other/doomsday.py --- other/doomsday.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/other/doomsday.py b/other/doomsday.py index 7c810d99f522..0d670b260f1a 100644 --- a/other/doomsday.py +++ b/other/doomsday.py @@ -1,9 +1,9 @@ #!/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 = { +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", From 7a57d7633c2dda76678008d3f365219f60fb4f7b Mon Sep 17 00:00:00 2001 From: John Law Date: Tue, 6 Oct 2020 23:03:43 +0800 Subject: [PATCH 8/9] Update doomsday.py --- other/doomsday.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/other/doomsday.py b/other/doomsday.py index 0d670b260f1a..afdbe340aa08 100644 --- a/other/doomsday.py +++ b/other/doomsday.py @@ -37,12 +37,12 @@ def get_week_day(year: int, month: int, day: int) -> str: (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor ) % 7 day_anchor = ( - _doomsday_not_leap[month - 1] + DOOMSDAY_NOT_LEAP[month - 1] if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) - else _doomsday_leap[month - 1] + else DOOMSDAY_LEAP[month - 1] ) week_day = (dooms_day + day - day_anchor) % 7 - return _week_day_names[week_day] + return WEEK_DAY_NAMES[week_day] if __name__ == "__main__": From 4c327e898b913ae7f957a8c6e0e2d72c844b7ad0 Mon Sep 17 00:00:00 2001 From: Ron U Date: Tue, 6 Oct 2020 21:59:54 +0300 Subject: [PATCH 9/9] adding more doctests (following review comment) [doomsday] --- other/doomsday.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/other/doomsday.py b/other/doomsday.py index afdbe340aa08..d8fe261156a1 100644 --- a/other/doomsday.py +++ b/other/doomsday.py @@ -21,6 +21,14 @@ def get_week_day(year: int, month: int, day: int) -> str: 'Saturday' >>> get_week_day(2017, 10, 24) 'Tuesday' + >>> get_week_day(2019, 5, 3) + 'Friday' + >>> get_week_day(1970, 9, 16) + 'Wednesday' + >>> get_week_day(1870, 8, 13) + 'Saturday' + >>> get_week_day(2040, 3, 14) + 'Wednesday' """ # minimal input check: