Last active
February 7, 2017 21:47
-
-
Save jfinucane/c33f48a0263a09cede0c56c63ebbceaf to your computer and use it in GitHub Desktop.
Average time of day
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fixme x | |
y=x.split(':') | |
y[0].to_i + y[1].to_i/60.0 + y[2].to_i/360.0 | |
end | |
def formatted x | |
hh = x.to_i | |
mm_pct = (x - hh) * 60 | |
mm = mm_pct.to_i | |
ss_pct = (mm_pct - mm) * 60 | |
ss = ss_pct.to_i | |
"%02d:%02d:%02d" % [hh,mm,ss] | |
end | |
def average x | |
sin_total = 0 | |
cos_total = 0 | |
x.each do |s| | |
y = fixme s | |
arc = (y/24.0) * 2* Math::PI | |
sin_total += Math.sin(arc) | |
cos_total += Math.cos(arc) | |
end | |
z = Math.atan2(sin_total, cos_total) | |
time = 12*(z/Math::PI) | |
fixnum = time > 0 ? time : time + 24 | |
formatted fixnum | |
end | |
a = ["21:00:00", "22:45:00", "21:35:00", "00:55:00", "20:50:00"] | |
# 22:10:49 (edited) | |
puts average a | |
b=["21:00:00", "22:35:00", "23:15:00", "00:00:00", "01:15:00"] | |
# 23:13:19 | |
puts average b | |
c =["10:30:00", "12:15:00", "11:25:00", "13:45:00", "09:40:00"] | |
# 11:30:24 | |
puts average c | |
d = ["01:00:00", "11:00:00", "12:00:00", "13:00:00", "23:00:00"] | |
# 11:59:59 | |
puts average d | |
e = ['01:00:00', '23:00:00'] | |
puts average e | |
f = ['01:00:00', '11:00:00'] | |
puts average f | |
g = ['00:00:00', '12:00:00'] #artifact of finite precision Math::PI | |
puts average g |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment