-
-
Save mikehodgson/5194d2a4659cac5369bd to your computer and use it in GitHub Desktop.
This file contains 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
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"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment