7 kyu

Differential Averaging

1,023 of 1,560wthit56

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.

Mathematics
Algebra
Algorithms

Stats:

CreatedDec 31, 2013
PublishedJan 25, 2014
Warriors Trained2770
Total Skips194
Total Code Submissions3123
Total Times Completed1560
JavaScript Completions1023
C# Completions102
C++ Completions224
Python Completions258
Total Stars23
% of votes with a positive feedback rating85% of 232
Total "Very Satisfied" Votes171
Total "Somewhat Satisfied" Votes51
Total "Not Satisfied" Votes10
Total Rank Assessments8
Average Assessed Rank
7 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • wthit56 Avatar
  • Dentzil Avatar
  • user5036852 Avatar
  • Voile Avatar
  • hobovsky Avatar
  • albertogcmr Avatar
  • ejini战神 Avatar
Ad