Skip to content

Instantly share code, notes, and snippets.

@jbgutierrez
Created October 16, 2009 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbgutierrez/211888 to your computer and use it in GitHub Desktop.
Save jbgutierrez/211888 to your computer and use it in GitHub Desktop.
Solution for the Rubylearning Challenge #2 (http://tinyurl.com/ydyc2kv)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment