-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_1.py
More file actions
98 lines (64 loc) · 3.2 KB
/
task_1.py
File metadata and controls
98 lines (64 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from datetime import datetime, timedelta
from collections import defaultdict
import calendar
def get_birthdays_per_week(users: {}) -> None:
"""
Based on input dictionary print peoples who's birthday is within next week
Logic:
In case today is Monday - we print all birthdays for next 7 days ignoring weekends
In case today is not Monday - we print all birthdays for next 7 days moving weekend birthday to Monday
Args:
users (dictionary): Birthday catalog (name and date)
Format:
{ "name": "<name>", "birthday": datetime() },
Sample:
{ "name": "Bill Foolkland", "birthday": datetime(1955, 10, 28) },
Output:
Print in console who's birthday within this week
Sample:
Monday: Bob, Lily Gates, Lily Evans
Tuesday: Geremy Evans
Wednesday: Geremy Gates
Thursday: Karter Gates
Friday: Karter Gates
"""
# const
DAY_OF_WEEK_CODE_FRIDAY = 4
DAY_OF_WEEK_CODE_MONDAY = 0
today_date = datetime.today().date()
is_monday = today_date.weekday()
date_range_upper_limit = today_date + timedelta(days=7)
birthdays_todo = defaultdict(list)
for user in users:
name = user["name"]
if name == "":
print(f"Warning: name is empty. Birthday: {str(birthday)}")
birthday_str = user["birthday"]
try:
birthday = birthday_str.date()
except:
raise(f"Something wrong with date for value {birthday_str}")
birthday_this_year = birthday.replace(year=today_date.year)
# case: today_date is monday
# calculate all birthday up to Friday (ignore Sat,Sun)
if is_monday and (today_date <= birthday_this_year
and birthday_this_year < date_range_upper_limit):
weekday_code = birthday_this_year.weekday()
if weekday_code <= DAY_OF_WEEK_CODE_FRIDAY:
weekday_name = calendar.day_name[weekday_code]
birthdays_todo[weekday_name].append(name)
# case: today_date is not monday
# calculate all birthday for 7 days (Sat,Sun move to monday)
if (not is_monday) and (today_date <= birthday_this_year
and birthday_this_year < date_range_upper_limit):
weekday_code = birthday_this_year.weekday()
if weekday_code <= DAY_OF_WEEK_CODE_FRIDAY:
weekday_name = calendar.day_name[weekday_code]
else:
# move birthday to Monday
weekday_name = calendar.day_name[DAY_OF_WEEK_CODE_MONDAY]
birthdays_todo[weekday_name].append(name)
# print results
for dow,names in birthdays_todo.items():
list_str = ", ".join(names)
print(f"{dow}: {list_str}")