secret
Last active

  • Download Gist
jirenpatel.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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 
require 'time'
 
class AverageTimeFinder
def average_time_of_day(actual_time_str, time_array)
sum = 0
check_format(actual_time_str)
actual_time = Time.parse(actual_time_str)
if(time_array.kind_of? Array)
time_array.each do |t|
check_format(t)
diff = (Time.parse(t) - actual_time )
diff = 24*60*60 + diff if diff < 0
sum = sum + diff
end
else
p "Plese give input proper time array"
exit
end
(actual_time + (sum/time_array.length)).strftime("%I:%M%p")
end
private
def check_format(time_str)
if (time_str.downcase.scan(/^\d+:\d+[ap]m/)[0] == nil)
p "Enter valid format (i.e 11:01am ,12:05pm, 10:0pm) for ", time_str
exit
end
true
end
end
 
 
 
flight_times = ["12:11am","11:51pm", "11:56pm", "12:01am", "12:06am"]
#flight_times = ["6:41am", "6:51am", "7:01am"]
 
a = AverageTimeFinder.new
print "Average departure/arrival time = " + a.average_time_of_day("10:0pm",flight_times) + "\n"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.