Skip to content

Instantly share code, notes, and snippets.

@wishdev
Created June 17, 2009 23:12
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 wishdev/131578 to your computer and use it in GitHub Desktop.
Save wishdev/131578 to your computer and use it in GitHub Desktop.
Weighted Average
days = 5
data = [ 25.0000, 24.8750, 24.7813, 24.5938, 24.5000, 24.6250, 25.2188, 27.2500, 26.2500, 26.5938 ]
avg_data = Array.new(data.length) { |pos|
tot = 0
# we check to see if we have enough days left to do a full average - otherwise use whatever we have left
day_count = days + pos < data.length ? days : data.length - pos
day_count.times { |day|
tot += data[pos + day] * (day_count - day)
}
tot / (day_count * (day_count +1) / 2)
}
puts avg_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment