Finished
1from datetime import date
1from datetime import date, timedelta
22
33
44# subclassing the built-in ValueError to create MeetupDayException
88 message: explanation of the error.
99
1010 """
11 def __init__(self) -> None:
12 pass
11 def __init__(self, message="That day does not exist."):
12 self.message = message
13 super().__init__(self.message)
1314
1415
1516def meetup(year: int, month: int, week: str, day_of_week: str) -> date:
16 pass
17 # Edge Case: Invalid month
18 if month < 1 or month > 12:
19 raise MeetupDayException("Invalid month")
20
21 # Edge Case: Invalid year
22 if year < 1:
23 raise MeetupDayException("Invalid year")
24
25 # Map day names to weekday numbers (Monday=0, Sunday=6)
26 days_map = {
27 'Monday': 0,
28 'Tuesday': 1,
29 'Wednesday': 2,
30 'Thursday': 3,
31 'Friday': 4,
32 'Saturday': 5,
33 'Sunday': 6
34 }
35
36 # Edge Case: Invalid day_of_week
37 if day_of_week not in days_map:
38 raise MeetupDayException("Invalid day of week")
39
40 target_weekday = days_map[day_of_week]
41
42 # Handle different week descriptors
43 if week == 'teenth':
44 # Check days 13-19
45 for day in range(13, 20):
46 candidate = date(year, month, day)
47 if candidate.weekday() == target_weekday:
48 return candidate
49 # This should never happen with valid inputs
50 raise MeetupDayException("That day does not exist.")
51
52 elif week == 'last':
53 # Start from the last day of the month and work backwards
54 if month == 12:
55 next_month = date(year + 1, 1, 1)
56 else:
57 next_month = date(year, month + 1, 1)
58 last_day = next_month - timedelta(days=1)
59
60 # Go back until we find the correct weekday
61 current = last_day
62 while current.month == month:
63 if current.weekday() == target_weekday:
64 return current
65 current -= timedelta(days=1)
66 # This should never happen with valid inputs
67 raise MeetupDayException("That day does not exist.")
68
69 else:
70 # Handle first, second, third, fourth, fifth
71 week_map = {
72 'first': 1,
73 'second': 2,
74 'third': 3,
75 'fourth': 4,
76 'fifth': 5
77 }
78
79 # Edge Case: Invalid week descriptor
80 if week not in week_map:
81 raise MeetupDayException("Invalid week descriptor")
82
83 occurrence = week_map[week]
84
85 # Find all occurrences of the weekday in the month
86 found_count = 0
87 # Start from the first day of the month
88 current = date(year, month, 1)
89
90 # If the first is after the target weekday, move to next week
91 while current.month == month:
92 if current.weekday() == target_weekday:
93 found_count += 1
94 if found_count == occurrence:
95 return current
96 current += timedelta(days=1)
97
98 # Edge Case: Requested occurrence does not exist (e.g., fifth Monday in a month with only 4 Mondays)
99 raise MeetupDayException("That day does not exist.")
100
101# Handled Edge Cases: Invalid month, Invalid year, Invalid day_of_week, Invalid week descriptor, Requested occurrence does not exist
Test NameStatus
test_fifth_monday_of_march_2015
Pass
test_fifth_saturday_of_february_2020
Pass
test_fifth_thursday_of_february_2024
Pass
test_last_friday_in_a_month_with_five_fridays
Pass
test_last_friday_in_a_month_with_four_fridays
Pass
test_last_monday_in_a_month_with_five_mondays
Pass
test_last_monday_in_a_month_with_four_mondays
Pass
test_last_saturday_in_a_month_with_four_saturdays
Pass
test_last_saturday_in_another_month_with_four_saturdays
Pass
test_last_sunday_in_a_month_with_five_sundays
Pass
test_last_sunday_in_a_month_with_four_sundays
Pass
test_last_sunday_of_june_2024
Pass
test_last_thursday_in_a_month_with_five_thursdays
Pass
test_last_thursday_in_a_month_with_four_thursdays
Pass
test_last_tuesday_in_a_month_with_four_tuesdays
Pass
test_last_tuesday_in_another_month_with_four_tuesdays
Pass
test_last_wednesday_in_a_month_with_five_wednesdays
Pass
test_last_wednesday_in_a_month_with_four_wednesdays
Pass
test_last_wednesday_in_december_that_is_also_the_last_day_of_the_year
Pass
test_nonexistent_fifth_friday_of_august_2022
Pass
test_nonexistent_fifth_monday_of_february_2022
Pass
test_nonexistent_fifth_thursday_of_may_2023
Pass
test_teenth_friday_of_may_2022
Pass
test_when_first_friday_is_some_day_in_the_middle_of_the_first_week
Pass
test_when_first_friday_is_the_1st_the_first_day_of_the_first_week
Pass
test_when_first_friday_is_the_7th_the_last_day_of_the_first_week
Pass
test_when_first_monday_is_some_day_in_the_middle_of_the_first_week
Pass
test_when_first_monday_is_the_1st_the_first_day_of_the_first_week
Pass
test_when_first_saturday_is_another_day_in_the_middle_of_the_first_week
Pass
test_when_first_saturday_is_some_day_in_the_middle_of_the_first_week
Pass
test_when_first_sunday_is_some_day_in_the_middle_of_the_first_week
Pass
test_when_first_sunday_is_the_7th_the_last_day_of_the_first_week
Pass
test_when_first_thursday_is_another_day_in_the_middle_of_the_first_week
Pass
test_when_first_thursday_is_some_day_in_the_middle_of_the_first_week
Pass
test_when_first_tuesday_is_some_day_in_the_middle_of_the_first_week
Pass
test_when_first_tuesday_is_the_7th_the_last_day_of_the_first_week
Pass
test_when_first_wednesday_is_some_day_in_the_middle_of_the_first_week
Pass
test_when_first_wednesday_is_the_7th_the_last_day_of_the_first_week
Pass
test_when_fourth_friday_is_some_day_in_the_middle_of_the_fourth_week
Pass
test_when_fourth_friday_is_the_22nd_the_first_day_of_the_fourth_week
Pass
test_when_fourth_monday_is_some_day_in_the_middle_of_the_fourth_week
Pass
test_when_fourth_monday_is_the_22nd_the_first_day_of_the_fourth_week
Pass
test_when_fourth_saturday_is_another_day_in_the_middle_of_the_fourth_week
Pass
test_when_fourth_saturday_is_some_day_in_the_middle_of_the_fourth_week
Pass
test_when_fourth_sunday_is_some_day_in_the_middle_of_the_fourth_week
Pass
test_when_fourth_sunday_is_the_28th_the_last_day_of_the_fourth_week
Pass
test_when_fourth_thursday_is_another_day_in_the_middle_of_the_fourth_week
Pass
test_when_fourth_thursday_is_some_day_in_the_middle_of_the_fourth_week
Pass
test_when_fourth_tuesday_is_some_day_in_the_middle_of_the_fourth_week
Pass
test_when_fourth_tuesday_is_the_28th_the_last_day_of_the_fourth_week
Pass
test_when_fourth_wednesday_is_some_day_in_the_middle_of_the_fourth_week
Pass
test_when_fourth_wednesday_is_the_28th_the_last_day_of_the_fourth_week
Pass
test_when_last_sunday_in_february_in_a_non_leap_year_is_not_the_29th
Pass
test_when_last_wednesday_in_february_in_a_leap_year_is_the_29th
Pass
test_when_second_friday_is_some_day_in_the_middle_of_the_second_week
Pass
test_when_second_friday_is_the_8th_the_first_day_of_the_second_week
Pass
test_when_second_monday_is_some_day_in_the_middle_of_the_second_week
Pass
test_when_second_monday_is_the_8th_the_first_day_of_the_second_week
Pass
test_when_second_saturday_is_another_day_in_the_middle_of_the_second_week
Pass
test_when_second_saturday_is_some_day_in_the_middle_of_the_second_week
Pass
test_when_second_sunday_is_some_day_in_the_middle_of_the_second_week
Pass
test_when_second_sunday_is_the_14th_the_last_day_of_the_second_week
Pass
test_when_second_thursday_is_another_day_in_the_middle_of_the_second_week
Pass
test_when_second_thursday_is_some_day_in_the_middle_of_the_second_week
Pass
test_when_second_tuesday_is_some_day_in_the_middle_of_the_second_week
Pass
test_when_second_tuesday_is_the_14th_the_last_day_of_the_second_week
Pass
test_when_second_wednesday_is_some_day_in_the_middle_of_the_second_week
Pass
test_when_second_wednesday_is_the_14th_the_last_day_of_the_second_week
Pass
test_when_teenth_friday_is_some_day_in_the_middle_of_the_teenth_week
Pass
test_when_teenth_friday_is_the_13th_the_first_day_of_the_teenth_week
Pass
test_when_teenth_friday_is_the_19th_the_last_day_of_the_teenth_week
Pass
test_when_teenth_monday_is_some_day_in_the_middle_of_the_teenth_week
Pass
test_when_teenth_monday_is_the_13th_the_first_day_of_the_teenth_week
Pass
test_when_teenth_monday_is_the_19th_the_last_day_of_the_teenth_week
Pass
test_when_teenth_saturday_is_some_day_in_the_middle_of_the_teenth_week
Pass
test_when_teenth_saturday_is_the_13th_the_first_day_of_the_teenth_week
Pass
test_when_teenth_saturday_is_the_19th_the_last_day_of_the_teenth_week
Pass
test_when_teenth_sunday_is_some_day_in_the_middle_of_the_teenth_week
Pass
test_when_teenth_sunday_is_the_13th_the_first_day_of_the_teenth_week
Pass
test_when_teenth_sunday_is_the_19th_the_last_day_of_the_teenth_week
Pass
test_when_teenth_thursday_is_some_day_in_the_middle_of_the_teenth_week
Pass
test_when_teenth_thursday_is_the_13th_the_first_day_of_the_teenth_week
Pass
test_when_teenth_thursday_is_the_19th_the_last_day_of_the_teenth_week
Pass
test_when_teenth_tuesday_is_some_day_in_the_middle_of_the_teenth_week
Pass
test_when_teenth_tuesday_is_the_13th_the_first_day_of_the_teenth_week
Pass
test_when_teenth_tuesday_is_the_19th_the_last_day_of_the_teenth_week
Pass
test_when_teenth_wednesday_is_some_day_in_the_middle_of_the_teenth_week
Pass
test_when_teenth_wednesday_is_the_13th_the_first_day_of_the_teenth_week
Pass
test_when_teenth_wednesday_is_the_19th_the_last_day_of_the_teenth_week
Pass
test_when_third_friday_is_some_day_in_the_middle_of_the_third_week
Pass
test_when_third_friday_is_the_15th_the_first_day_of_the_third_week
Pass
test_when_third_monday_is_some_day_in_the_middle_of_the_third_week
Pass
test_when_third_monday_is_the_15th_the_first_day_of_the_third_week
Pass
test_when_third_saturday_is_another_day_in_the_middle_of_the_third_week
Pass
test_when_third_saturday_is_some_day_in_the_middle_of_the_third_week
Pass
test_when_third_sunday_is_some_day_in_the_middle_of_the_third_week
Pass
test_when_third_sunday_is_the_21st_the_last_day_of_the_third_week
Pass
test_when_third_thursday_is_another_day_in_the_middle_of_the_third_week
Pass
test_when_third_thursday_is_some_day_in_the_middle_of_the_third_week
Pass
test_when_third_tuesday_is_some_day_in_the_middle_of_the_third_week
Pass
test_when_third_tuesday_is_the_21st_the_last_day_of_the_third_week
Pass
test_when_third_wednesday_is_some_day_in_the_middle_of_the_third_week
Pass
test_when_third_wednesday_is_the_21st_the_last_day_of_the_third_week
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.