Differential Averaging
Description:
Say you have a ratings system. People can rate a page, and the average is displayed on the page for everyone to see.
One way of storing such a running average is to keep the the current average as well as the total rating that all users have submitted and with how many people rated it, so that the average can be calculated and updated when a new rating has been made.
There are a couple of minor problems with this: first, you're keeping 3 columns instead of 1, which isn't ideal. Second is, if you're not careful, the number could get too large and get less and less accurate as the data format tries to keep up.
So what you need to do is this: write a function that takes the current
average, the current number of ratings (data points
) made, and a new value to add
to the average; then return the new value. That way, you only need 2 columns in your database, and the number will not get crazy large over time.
To be clear:
current = 0.5
points = 2
add = 1
--> 0.6666666666666666666666666666666666 // (2/3)
There are also plenty of examples in the example tests.
Similar Kata:
Stats:
Created | Dec 31, 2013 |
Published | Jan 25, 2014 |
Warriors Trained | 2770 |
Total Skips | 194 |
Total Code Submissions | 3123 |
Total Times Completed | 1560 |
JavaScript Completions | 1023 |
C# Completions | 102 |
C++ Completions | 224 |
Python Completions | 258 |
Total Stars | 23 |
% of votes with a positive feedback rating | 85% of 232 |
Total "Very Satisfied" Votes | 171 |
Total "Somewhat Satisfied" Votes | 51 |
Total "Not Satisfied" Votes | 10 |
Total Rank Assessments | 8 |
Average Assessed Rank | 7 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 8 kyu |