Create a gist now

Instantly share code, notes, and snippets.

Solution for the Rubylearning Challenge #2 (
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
average = sum / args.length"%l:%M%p").strip.downcase
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"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment