secret
Last active

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12
require 'time'
 
def average_time_of_day(array_of_times)
total = array_of_times.inject(0) { |total,current| total + Time.parse(current).to_i }
Time.at(total / array_of_times.size).strftime('%I:%M %p').downcase!
end
 
# Assumes same day
puts average_time_of_day(["12:00 am", "11:59 pm", "11:53 pm"])
 
# Also works
puts average_time_of_day(["Nov 2 12:00 am", "Nov 1 11:59 pm", "Nov 1 11:53 pm"])

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.