-
-
Save jiren/b0dde1717ef9f15ef9c4 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' | |
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" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment