public
Last active

Solution for the Rubylearning Challenge #2 (http://tinyurl.com/ydyc2kv)

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
require 'time'
def average_time_of_day(args)
sum = 0
args.each do |arg|
sum += Time.parse(arg).to_i
sum += 60 * 60 * 24 unless arg =~ /^1[01].*pm$/ # My friend arrival is scheduled at 10pm so any time outside 10:00pm and 11:59pm means he arrived on the very next day
end
average = sum / args.length
Time.at(average).strftime("%l:%M%p").strip.downcase
end
require "test/unit"
class TestAlgorithm < Test::Unit::TestCase
def test_program_output
assert_equal("12:01am", average_time_of_day(["11:51pm", "11:56pm", "12:01am", "12:06am", "12:11am"]))
assert_equal("6:51am", average_time_of_day(["6:41am", "6:51am", "7:01am"]))
assert_equal("6:01am", average_time_of_day(["12:02am", "12:00pm"]))
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.