Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
meh solution in Clear category for Lightbulb End Watching by Leonix
from datetime import datetime
def sum_light(els, start_watching = None, end_watching = None):
# Split into pairs (make sure times are even)
if end_watching is not None and len(els) % 2 == 1:
els.append(end_watching)
pairs = (els[i:i+2] for i in range(0, len(els), 2))
# Respect watching start and end
if start_watching is not None:
pairs = ([max(time_on, start_watching), time_off]
for time_on, time_off in pairs
if time_off > start_watching)
if end_watching is not None:
pairs = ([time_on, min(end_watching, time_off)]
for time_on, time_off in pairs
if time_on < end_watching)
# Calculate total lit time
return sum((time_off - time_on).total_seconds()
for time_on, time_off in pairs)
May 14, 2021
Comments: