Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
itertools solution in Clear category for Weekend Counter by marcopunteri
from datetime import date, timedelta
from itertools import takewhile, count, filterfalse
def checkio(from_date, to_date):
days = (from_date + timedelta(days=day) for day in count(0,1))
weekend_days = filterfalse(lambda x: x.weekday() < 5, takewhile(lambda x: x <= to_date, days))
return len(list(weekend_days))
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(date(2013, 9, 18), date(2013, 9, 23)) == 2, "1st example"
assert checkio(date(2013, 1, 1), date(2013, 2, 1)) == 8, "2nd example"
assert checkio(date(2013, 2, 2), date(2013, 2, 3)) == 2, "3rd example"
May 7, 2021