Skip to content

Instantly share code, notes, and snippets.

@Ausjorg
Created January 24, 2024 23:07
Show Gist options
  • Save Ausjorg/4a61c1adacefb12d222aba106897a5ca to your computer and use it in GitHub Desktop.
Save Ausjorg/4a61c1adacefb12d222aba106897a5ca to your computer and use it in GitHub Desktop.
This Python code defines a function add_times that takes a list of time strings in "hours:minutes:seconds" format, calculates the total time by converting each time string to seconds and summing them up, and then converts the total time back into the same "hours:minutes:seconds" format.
def add_times(time_array):
total_seconds = sum(
sum(map(lambda x, y: int(x) * y, time_str.split(':'), [3600, 60, 1]))
for time_str in time_array
)
hours, remainder = divmod(total_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
return f"{hours}:{minutes:02d}:{seconds:02d}"
# Example usage
time_array = ["1:30:40", "2:15:30", "0:45:55"]
result = add_times(time_array)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment