Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Weekstart shift solution in Clear category for Weekly Calendar by CDG.Axel
from typing import List
from datetime import date, timedelta
def weekly_calendar(year: int, month: int, day: int, firstweekday: int) -> List[int]:
dt = date(year, month, day)
return [(dt + timedelta(i - (dt.weekday() - firstweekday) % 7)).day for i in range(7)]
"""
def weekly_calendar(year: int, month: int, day: int, firstweekday: int) -> List[int]:
dt = date(year, month, day)
# calculate how many days before was then start of the week (0 - week start at dt)
shift = (dt.weekday() - firstweekday) % 7
return [(dt + timedelta(i - shift)).day for i in range(7)]
"""
if __name__ == '__main__':
print("Example:")
print(list(weekly_calendar(2020, 1, 1, 0)))
# These "asserts" are used for self-checking and not for an auto-testing
assert list(weekly_calendar(2020, 1, 1, 0)) == [30, 31, 1, 2, 3, 4, 5], "01/01/2020 Monday"
assert list(weekly_calendar(2020, 9, 20, 6)) == [20, 21, 22, 23, 24, 25, 26], "09/20/2020 Sunday"
assert list(weekly_calendar(2020, 9, 30, 0)) == [28, 29, 30, 1, 2, 3, 4], "09/30/2020 Monday"
assert list(weekly_calendar(2020, 2, 29, 2)) == [26, 27, 28, 29, 1, 2, 3], "02/29/2020 Wednesday"
print("Coding complete? Click 'Check' to earn cool rewards!")
Dec. 5, 2021
Comments: