secret
anonymous / davidjenkins.rb
Last active

  • Download Gist
davidjenkins.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
=begin
Purpose:write method average_time_of_day that takes an array of times in format "hh:mm(am|pm)"
and returns the average time of those times in the same format
e.g.,
>> average_time_of_day(["11:51pm", "11:56pm", "12:01am", "12:06am", "12:11am"])
=> "12:01am"
Notes : program assumes that the first time in the array is the earliest actual time in the array
Author: David Jenkins
=end
 
require 'time'
 
ONE_DAY_IN_SECONDS = (60 * 60 * 24)
 
def average_time_of_day times
first_time = nil
time_sum = 0
times.each do |t|
mil_time = Time.parse(t)
first_time ||= mil_time
if mil_time < first_time
mil_time+= ONE_DAY_IN_SECONDS
end
time_sum += mil_time.to_i
end
Time.at(time_sum / times.size).strftime("%I:%M%p").downcase
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.