Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Concise algorithm solution in Speedy category for Lightbulb End Watching by Igor_Sekretarev
from datetime import datetime
from typing import List, Optional
def sum_light(
els: List[datetime],
start_watching: Optional[datetime] = None,
end_watching: Optional[datetime] = None
) -> int:
if len(els) % 2:
els.append(datetime.max)
if start_watching is None:
start_watching = els[0]
if end_watching is None:
end_watching = datetime.max
return sum(
(min(end_watching, els[i+1]) - max(start_watching, els[i])).total_seconds()
for i in range(0, len(els), 2)
if start_watching <= els[i+1] and end_watching >= els[i]
)
if __name__ == '__main__':
print("Example:")
print(sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
],
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10)))
assert sum_light(els=[
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
],
start_watching=datetime(2015, 1, 12, 10, 0, 0),
end_watching=datetime(2015, 1, 12, 10, 0, 10)) == 10
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
],
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 7)) == 7
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
],
datetime(2015, 1, 12, 10, 0, 3),
datetime(2015, 1, 12, 10, 0, 10)) == 7
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
],
datetime(2015, 1, 12, 10, 0, 10),
datetime(2015, 1, 12, 10, 0, 20)) == 0
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
datetime(2015, 1, 12, 11, 10, 10),
],
datetime(2015, 1, 12, 10, 30, 0),
datetime(2015, 1, 12, 11, 0, 0)) == 0
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
datetime(2015, 1, 12, 11, 10, 10),
],
datetime(2015, 1, 12, 10, 10, 0),
datetime(2015, 1, 12, 11, 0, 0)) == 10
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
datetime(2015, 1, 12, 11, 10, 10),
],
datetime(2015, 1, 12, 10, 10, 0),
datetime(2015, 1, 12, 11, 0, 10)) == 20
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
datetime(2015, 1, 12, 11, 10, 10),
],
datetime(2015, 1, 12, 9, 50, 0),
datetime(2015, 1, 12, 10, 0, 10)) == 10
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
datetime(2015, 1, 12, 11, 10, 10),
],
datetime(2015, 1, 12, 9, 0, 0),
datetime(2015, 1, 12, 10, 5, 0)) == 300
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
datetime(2015, 1, 12, 11, 10, 10),
],
datetime(2015, 1, 12, 11, 5, 0),
datetime(2015, 1, 12, 12, 0, 0)) == 310
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
],
datetime(2015, 1, 12, 11, 5, 0),
datetime(2015, 1, 12, 11, 10, 0)) == 300
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
],
datetime(2015, 1, 12, 10, 10, 0),
datetime(2015, 1, 12, 11, 0, 10)) == 20
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
],
datetime(2015, 1, 12, 9, 10, 0),
datetime(2015, 1, 12, 10, 20, 20)) == 610
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
],
datetime(2015, 1, 12, 9, 10, 0),
datetime(2015, 1, 12, 10, 20, 20)) == 1220
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
],
datetime(2015, 1, 12, 9, 9, 0),
datetime(2015, 1, 12, 10, 0, 0)) == 0
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
],
datetime(2015, 1, 12, 9, 9, 0),
datetime(2015, 1, 12, 10, 0, 10)) == 10
print("The third mission in series is completed? Click 'Check' to earn cool rewards!")
May 5, 2021