From anonymous, 1 Month ago, written in Python.
Embed
  1. >>> from datetime import date
  2. >>> dates = [date(2022, 10, 5), date(2022, 10, 6), date(2022, 10, 7), date(2022, 10, 8), date(2022, 10, 10), date(2022, 10, 11)]
  3. >>> len(list(itertools.dropwhile(lambda ds: (ds[1]-ds[0]).days == 1, zip(dates, dates[1:]))))
  4. 2
  5. >>>
  6. >>> # to understand the steps it does:
  7. >>>
  8. >>> list(zip(dates, dates[1:]))  # get pairs of first/second, second/third, etc.
  9. [
  10.     (datetime.date(2022, 10, 5), datetime.date(2022, 10, 6)),
  11.     (datetime.date(2022, 10, 6), datetime.date(2022, 10, 7)),
  12.     (datetime.date(2022, 10, 7), datetime.date(2022, 10, 8)),
  13.     (datetime.date(2022, 10, 8), datetime.date(2022, 10, 10)),
  14.     (datetime.date(2022, 10, 10), datetime.date(2022, 10, 11))
  15. ]
  16. >>> list(itertools.dropwhile(lambda ds: (ds[1]-ds[0]).days == 1, zip(dates, dates[1:])))  # drop as long as the difference in days is 1
  17. [(datetime.date(2022, 10, 8), datetime.date(2022, 10, 10)), (datetime.date(2022, 10, 10), datetime.date(2022, 10, 11))]
  18. >>>
  19. >>> # then, finally, get the length of that (two pairs -> two days)