secret
anonymous / bradoconnor.rb
Last active

  • Download Gist
bradoconnor.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 47 48
require 'time'
 
class FlightTimes
attr_reader :times
def initialize (times)
@times = times
end
def sort!
@times.sort!
end
def best_start_time #Returns the index number of the @times array that gives the shortest interval between start and end times
times_array = @times
result_array = []
times_array.each do
result_array << times_array.last - times_array.first
self.cycle_times
end
result_array.index(result_array.min)
end
def cycle_times #Cycles the array of @times so the first time becomes the last and is advanced to tomorrow
@times << @times.shift+86400
end
def mean
sum=0.0
@times.each {|time| sum += time.to_f}
sum/@times.length
end
end
 
def average_time_of_day(timestrings)
times=[]
timestrings.each do |time|
if (time.class != String or (time =~ /([0-9]|01|02|03|04|05|06|07|08|09|10|11|12):[0-5]\d[a|p]m/) != 0)
raise ArgumentError
end
times << Time.parse(time)
end
flight_times = FlightTimes.new(times)
flight_times.sort!
flight_times.best_start_time.times do
flight_times.cycle_times
end
Time.at(flight_times.mean).strftime("%I:%M%p").sub(/^0/,"").downcase
rescue ArgumentError
"Invalid times entered. Times must be entered as an array of strings in 12 hour format with no leading zero, i.e. the format h:mm(am/pm), eg 1:20pm, 10:30am etc."
end
puts average_time_of_day(ARGV) unless ARGV.empty?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.