Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First: rebase els to start_watching, then proceed as in Lightbulb Intro solution in Clear category for Lightbulb Start Watching by leggewie
from datetime import datetime, timedelta
from typing import List, Optional
def sum_light(els: List[datetime], start_watching: Optional[datetime] = None) -> int:
"""
how long the light bulb has been turned on
"""
# rebase els as necessary to disregard all times before start_watching
if start_watching:
x = timedelta(seconds=0)
els = [max(x,t-start_watching) for t in els]
# els[0::2] is the array of start times and els[1::2] of the end times
seconds = sum(list(map(lambda start, end: int((end - start).total_seconds()), els[0::2], els[1::2])))
return seconds
May 30, 2021
Comments: