Skip to content

@flippingbits /ruby_challenge_2.rb secret
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.