Created
February 21, 2018 15:05
-
-
Save vlad-bezden/0afe988fb5bd7f914bda5863b71897d6 to your computer and use it in GitHub Desktop.
Calculates time from any time passed format H:M:S M:S S
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from datetime import timedelta | |
| from functools import reduce | |
| from itertools import chain, repeat | |
| from typing import List | |
| def calc(times: List[str]) -> timedelta: | |
| '''Calculates total time from the list of times''' | |
| result = timedelta() | |
| for t in times: | |
| for hms in [t.split(':')]: | |
| s, m, h, *_ = chain(hms[::-1], repeat('0', 3)) | |
| result += timedelta(hours=int(h), minutes=int(m), seconds=int(s)) | |
| return result |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of using it
times = [
'1:2:3',
'4:5',
'6'
]
total_time = calc(times) # 1:06:14