Create a gist now

Instantly share code, notes, and snippets.

anonymous /davidjenkins.rb Secret
Created Oct 9, 2009

What would you like to do?
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
>> 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
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
time_sum += mil_time.to_i
end / times.size).strftime("%I:%M%p").downcase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment