-
-
Save flippingbits/71001d595386112ed45c 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' | |
# define the regular arrival time, that is used to detect a cycle | |
REGULAR_ARRIVAL_TIME = "10:00pm" | |
# maximum delay can be 23 hours 59 minutes (because of cycling times) | |
def average_time_of_day(arrivals) | |
# check for a cycle and convert arrival times into UNIX timestamps | |
arrivals.collect!{ |a| | |
a = Time.parse(a) | |
a += 60*60*24 if a < Time.parse(REGULAR_ARRIVAL_TIME) | |
a.to_i | |
} | |
# get the average timestamp | |
sum = arrivals.inject{ |sum,x| sum + x } | |
average_time = sum/arrivals.length | |
# convert the timestamp back and return it | |
average_time = Time.at(average_time) | |
average_time.strftime("%I:%M%p") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment