Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
total_seconds solution in Clear category for Lightbulb Intro by Ash72
from datetime import datetime
from typing import List
def sum_light(els: List[datetime]) -> int:
"""
how long the light bulb has been turned on
"""
return sum((b - a).total_seconds() for a, b in zip(els[::2], els[1::2]))
if __name__ == "__main__":
print("Example:")
print(
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),
]
)
)
# These "asserts" are used for self-checking and not for an auto-testing
assert (
sum_light(
els=[
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
]
)
== 610
)
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),
]
)
== 1220
)
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, 10, 10),
datetime(2015, 1, 12, 12, 10, 10),
]
)
== 4820
)
assert (
sum_light(
[
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 1),
]
)
== 1
)
assert (
sum_light(
[
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
datetime(2015, 1, 12, 11, 0, 0),
datetime(2015, 1, 13, 11, 0, 0),
]
)
== 86410
)
print(
"The first mission in series is completed? Click 'Check' to earn cool rewards!"
)
May 21, 2023
Comments: