Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
28-liner: straightforward solution in Clear category for Next Birthday by przemyslaw.daniel
from datetime import datetime
from collections import defaultdict
from typing import Dict, Tuple
Date = Tuple[int, int, int]
def convert(year: int, month: int, day: int) -> datetime:
""" convert year, month, day to datetime """
try:
return datetime(year, month, day)
except ValueError: # handle special 1st march case
return datetime(year, 3, 1)
def next_birthday(today: Date, birthdates: Dict[str, Date]) -> Tuple[int, Dict[str, int]]:
""" find nearest birthdates """
today, result = datetime(*today), defaultdict(list)
for step in range(2): # day/month can be valid also for next year
for name, (year, month, day) in birthdates.items():
days = (convert(today.year + step, month, day) - today).days
result[days].append((name, today.year + step - year))
days = min(x for x in result if x >= 0)
return days, {name: age for name, age in result[days]}
Oct. 17, 2020
Comments: