Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Next Birthday solution in Clear category for Next Birthday by vvm70
from typing import Dict, Tuple
from datetime import date
Date = Tuple[int, int, int]
def next_birthday(today: Date, birthdates: Dict[str, Date]) -> Tuple[int, Dict[str, int]]:
nextbirthdays = []
for name, (year, month, day) in birthdates.items():
nextyear = today[0] + (month < today[1]) + (month == today[1] and day < today[2])
if (month, day) == (2, 29):
if today[1:] == (3, 1):
nextyear = today[0] + ((today[0] % 4) == 0)
if nextyear % 4:
month, day = 3, 1
nextbirthdays.append((((date(nextyear, month, day) - date(*today)).days), (name, nextyear - year)))
days = min(nextbirthdays, key=lambda x: x[0])[0]
return days, {name: age for d, (name, age) in nextbirthdays if d == days}
if __name__ == '__main__':
FAMILY = {
'Brian': (1967, 5, 31),
'Léna': (1970, 10, 3),
'Philippe': (1991, 6, 15),
'Yasmine': (1996, 2, 29),
'Emma': (2000, 12, 25),
}
TESTS = [
((2020, 9, 8), (25, {'Léna': 50})),
((2021, 10, 4), (82, {'Emma': 21})),
((2022, 3, 1), (0, {'Yasmine': 26})),
]
for nb, (day, answer) in enumerate(TESTS, 1):
user_result = tuple(next_birthday(day, FAMILY.copy()))
if user_result != answer:
print(f'You failed the test #{nb}.')
print(f'Your result: {user_result}')
print(f'Right result: {answer}')
break
else:
print('Well done! Click on "Check" for real tests.')
Oct. 19, 2020
Comments: