Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Working Hours Calculator by arun_maiti
from datetime import timedelta, date
def working_hours(
date1: str, date2: str, start_time: str, end_time: str, holy: list[str]
) -> int | float:
d1, d2 = map(date.fromisoformat, (date1, date2))
holy = tuple(map(date.fromisoformat, holy))
h1, m1 = map(int, start_time.split(":"))
h2, m2 = map(int, end_time.split(":"))
working_hours_day = timedelta(hours=h2-h1, minutes=m2-m1)
days = 0
while d1 <= d2:
if d1 not in holy and d1.isoweekday() < 6:
days += 1
d1 += timedelta(days=1)
return round(days * working_hours_day.seconds/3600, 2)
print("Example:")
print(working_hours("2023-03-01", "2023-03-01", "09:00", "17:00", []))
# These "asserts" are used for self-checking
assert working_hours("2023-03-01", "2023-03-01", "09:00", "17:00", []) == 8
assert working_hours("2023-03-01", "2023-03-02", "09:00", "17:00", []) == 16
assert working_hours("2023-03-01", "2023-03-03", "09:00", "17:00", ["2023-03-01"]) == 16
assert (
working_hours("2023-03-01", "2023-03-05", "08:45", "17:10", ["2023-03-03"]) == 16.83
)
print("The mission is done! Click 'Check Solution' to earn rewards!")
Dec. 5, 2025
Comments: