def meetup_day(year, month, weekday_str, day_order): class MeetupDayException(Exception): pass days_of_week = {'Monday': 0, 'Tuesday': 1, 'Wednesday': 2, 'Thursday': 3, 'Friday': 4, 'Saturday': 5, 'Sunday': 6} days_descriptions = {'1st': 1, '2nd': 2, '3rd': 3, '4th': 4, '5th': 5, 'last': 'last', 'teenth': (13, 14, 15, 16, 17, 18, 19)} #days_in_month = 0 weekday = days_of_week[weekday_str] # calculate all dates in month number_of_days = days_in_month(month, is_leap_year(year)) month_dates = [date(year, month, day) for day in range(1, number_of_days + 1)] # all weekdays in month month_weekdays = [(day, day.weekday()) for day in month_dates] required_day = days_descriptions[day_order] # collect all dates with required weekday all_dates_with_required_weekday = [] for item in month_weekdays: if weekday in item: all_dates_with_required_weekday.append(item[0]) # select required date using description if required_day == 'last': result = all_dates_with_required_weekday[-1] elif type(required_day) is int: if len(all_dates_with_required_weekday) >= required_day: result = all_dates_with_required_weekday[required_day - 1] else: raise MeetupDayException() elif type(required_day) is tuple: for item in all_dates_with_required_weekday: if item.day in required_day: return item raise MeetupDayException() # no such day else: raise MeetupDayException() # date not found return result
def test_century(self): self.assertIs(is_leap_year(1900), False)
def test_is_leap3(): with pytest.raises(ValueError): is_leap_year("fasdkjgfas")
def test_year_not_divisible_by_4(self): self.assertIs(is_leap_year(2015), False)
def test_year_divisible_by_400(self): self.assertIs(is_leap_year(2000), True)
def test_leap_year(self): self.assertTrue(is_leap_year(1996))
def test_leap_year(self): self.assertIs(is_leap_year(1996), True)
def test_year_divisible_by_100_but_not_divisible_by_400(self): self.assertIs(is_leap_year(200), False) self.assertIs(is_leap_year(1400), False) self.assertIs(is_leap_year(600), False) self.assertIs(is_leap_year(1900), False) self.assertIs(is_leap_year(1000), False)
def test_another_century(self): self.assertIs(is_leap_year(2000), True)
def test_correct(self): self.assertEqual(leap.is_leap_year(1600), "1600 is a leap year!") self.assertEqual(leap.is_leap_year(1996), "1996 is a leap year!") self.assertEqual(leap.is_leap_year(2000), "2000 is a leap year!")
def test_year_divisible_by_100_not_divisible_by_400(self): self.assertFalse(is_leap_year(2100))
def test_year_divisible_by_4_not_divisible_by_100(self): self.assertTrue(is_leap_year(2016))
def test_non_leap_year(self): self.assertFalse(is_leap_year(1997))
def test_year_divisible_by_4(self): self.assertIs(is_leap_year(1996), True) self.assertIs(is_leap_year(8), True) self.assertIs(is_leap_year(1412), True) self.assertIs(is_leap_year(1236), True)
def test_year_divisible_by_4_not_divisible_by_100(self): self.assertIs(is_leap_year(1996), True)
def test2(self): self.assertTrue(is_leap_year("2024"))
def test_year_1900(self): self.assertIs(is_leap_year(1900), False)
def test3(self): self.assertFalse(is_leap_year("1981"))
def test1(self): self.assertTrue(is_leap_year("2020"))
def test_year_divisible_by_200_not_divisible_by_400(self): self.assertIs(is_leap_year(1800), False)
def test_current_year_2018_is_not_a_leap_year(self): self.assertIs(is_leap_year(), False)
def test_is_leap2(): assert is_leap_year("2021") == False
def test_non_leap_even_year(self): self.assertFalse(leap.is_leap_year(1998))
def test_is_leap1(): assert is_leap_year("2020") == True
def test_century(self): self.assertFalse(leap.is_leap_year(1900))
def test_exceptional_century(self): self.assertIs(is_leap_year(2400), True)
def test_exceptional_century(self): self.assertTrue(leap.is_leap_year(2400))
def test_non_leap_year(self): self.assertIs(is_leap_year(1997), False)
def test_leap_year(self): self.assertTrue(leap.is_leap_year(1996))
def test_year_divisible_not_divisible(self): self.assertIs(is_leap_year(999), False)
def test_year_divisible_by_100_not_divisible_by_400(self): self.assertIs(is_leap_year(2100), False)
def test_year_divisible_by_2_not_divisible_by_4(self): self.assertIs(is_leap_year(1970), False)
def test_year_2016(self): self.assertIs(is_leap_year(2016), True)
def test_year_divisible_by_409990(self): self.assertIs(is_leap_year(1900), False)
def test_century(self): self.assertFalse(is_leap_year(1900))