# your code goes here from datetime import date, timedelta def getLastWed(d): if d.isoweekday() >= 3: delta = d.isoweekday() - 3 else: delta = d.isoweekday() + 7 - 3 return d - timedelta(days=delta) def getLast3WedInMonth(d): weeknum = d.isocalendar()[1] startWeeknum = date(d.year, d.month, 1).isocalendar()[1] if d.isoweekday() >= 3: thisWed = getLastWed(d) lastWed = getLastWed(thisWed - timedelta(days=1)) last2Wed = getLastWed(lastWed - timedelta(days=1)) else: thisWed = getLastWed(d) lastWed = thisWed last2Wed = getLastWed(lastWed - timedelta(days=1)) return list(filter(lambda x: x["weeknumber"] >= startWeeknum, [ {"weeknumber": weeknum-2, 'date': last2Wed}, {"weeknumber": weeknum-1, 'date': lastWed}, {"weeknumber": weeknum, 'date': thisWed} ])) print(getLast3WedInMonth(date.today())) print(getLast3WedInMonth(date(2019, 9, 11))) print(getLast3WedInMonth(date(2019, 9, 10))) print(getLast3WedInMonth(date(2019, 9, 4)))
Standard input is empty
[{'weeknumber': 35, 'date': datetime.date(2019, 8, 28)}, {'weeknumber': 36, 'date': datetime.date(2019, 9, 4)}, {'weeknumber': 37, 'date': datetime.date(2019, 9, 11)}] [{'weeknumber': 35, 'date': datetime.date(2019, 8, 28)}, {'weeknumber': 36, 'date': datetime.date(2019, 9, 4)}, {'weeknumber': 37, 'date': datetime.date(2019, 9, 11)}] [{'weeknumber': 35, 'date': datetime.date(2019, 8, 28)}, {'weeknumber': 36, 'date': datetime.date(2019, 9, 4)}, {'weeknumber': 37, 'date': datetime.date(2019, 9, 4)}] [{'weeknumber': 35, 'date': datetime.date(2019, 8, 28)}, {'weeknumber': 36, 'date': datetime.date(2019, 9, 4)}]