fork download
  1. # your code goes here
  2. from datetime import date, timedelta
  3.  
  4. def getLastWed(d):
  5. if d.isoweekday() >= 3:
  6. delta = d.isoweekday() - 3
  7. else:
  8. delta = d.isoweekday() + 7 - 3
  9. return d - timedelta(days=delta)
  10.  
  11. def getLast3WedInMonth(d):
  12. weeknum = d.isocalendar()[1]
  13. startWeeknum = date(d.year, d.month, 1).isocalendar()[1]
  14. if d.isoweekday() >= 3:
  15. thisWed = getLastWed(d)
  16. lastWed = getLastWed(thisWed - timedelta(days=1))
  17. last2Wed = getLastWed(lastWed - timedelta(days=1))
  18. else:
  19. thisWed = getLastWed(d)
  20. lastWed = thisWed
  21. last2Wed = getLastWed(lastWed - timedelta(days=1))
  22.  
  23. return list(filter(lambda x: x["weeknumber"] >= startWeeknum,
  24. [
  25. {"weeknumber": weeknum-2, 'date': last2Wed},
  26. {"weeknumber": weeknum-1, 'date': lastWed},
  27. {"weeknumber": weeknum, 'date': thisWed}
  28. ]))
  29.  
  30. print(getLast3WedInMonth(date.today()))
  31. print(getLast3WedInMonth(date(2019, 9, 11)))
  32. print(getLast3WedInMonth(date(2019, 9, 10)))
  33. print(getLast3WedInMonth(date(2019, 9, 4)))
  34.  
Success #stdin #stdout 0.02s 9396KB
stdin
Standard input is empty
stdout
[{'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)}]