secret
Last active

  • Download Gist
ruby_challenge_2.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.