Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
# A horribly scrappy "just for fun" entry into Satish Talim's latest Ruby
# challenge. Didn't want to use any libraries, etc, just bashed at it true 1980s style!
# It makes the assumption, as was raised in several comments, that the first time in
# the array is the first of the "block" of times..
def average_time_of_day(times)
pr = nil! do |t|
h,m = t.scan(/\d+/).map(&:to_i)
h += 12 if t =~ /pm/ || (pr && h < pr)
pr = h
h + m / 60.0
mean = times.reduce(:+) / times.size
h = mean.to_i
m = sprintf("%0.2d", ((mean - h) * 60.0).round)
"#{h > 12 ? h - 12 : h}:#{m}#{ h > 12 && h != 24 ? 'pm' : 'am' }"
raise unless average_time_of_day(["11:51pm", "11:56pm", "12:01am", "12:06am", "12:11am"]) == "12:01am"
raise unless average_time_of_day(["6:41am", "6:51am", "7:01am"]) == "6:51am"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.