Skip to content

Instantly share code, notes, and snippets.

@rmariano
Created November 28, 2021 14:17
Show Gist options
  • Save rmariano/fc3bfd3227fb46c53b184e4b58c2c5a3 to your computer and use it in GitHub Desktop.
Save rmariano/fc3bfd3227fb46c53b184e4b58c2c5a3 to your computer and use it in GitHub Desktop.
"""A small script to find all months that have a Monday 25th for a particular
year.
Input: year in format YYYY
Output: print list of dates that are a Monday 25 in that year
"""
import re
import sys
from typing import Iterator
from datetime import date
VALID_YEAR_FORMAT = re.compile(r"(\d{4})")
REQUIRED_DAY_NUMBER = 25
MONDAY = 0
def _get_raw_input() -> str:
if len(sys.argv) > 1:
return sys.argv[1]
return input("Enter year (YYYY): ")
def _validate_and_parse_raw_input(raw_input: str) -> int:
if year := re.match(VALID_YEAR_FORMAT, raw_input):
return int(year.group(0))
raise RuntimeError(f"{raw_input!r} doesn't comply with the format (YYYY)")
def parse_input() -> int:
raw_input = _get_raw_input()
return _validate_and_parse_raw_input(raw_input)
def _is_monday(a_date: date) -> bool:
return a_date.weekday() == MONDAY
def find_all_required(year: int) -> Iterator[date]:
for month in range(1, 12 + 1):
candidate = date(year, month, REQUIRED_DAY_NUMBER)
if _is_monday(candidate):
yield candidate
def display(dates: Iterator[date]):
for day in dates:
print(day.strftime("%A %d of %B, %Y"))
def main():
year: int = parse_input()
results_found = find_all_required(year)
display(results_found)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment