Skip to content

Instantly share code, notes, and snippets.

@ggarnier
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggarnier/9531423 to your computer and use it in GitHub Desktop.
Save ggarnier/9531423 to your computer and use it in GitHub Desktop.
Calculates sum of a list of time shares in format min:sec
# str = "1:42 + 07:48"
# output = "9:30"
def sum_time_shares(str)
values = str.gsub(" ", "").split("+")
total_seconds = values.reduce(0) do |sum, value|
minutes, seconds = value.split(":")
sum += (60 * minutes.to_i) + seconds.to_i
end
"#{total_seconds / 60}:#{total_seconds % 60}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment