Created
January 3, 2016 23:44
-
-
Save alantsai/aeae625d0eba703c3ff6 to your computer and use it in GitHub Desktop.
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
public static double WeightedAverage<T>(this IEnumerable<T> records, Func<T, double> value, Func<T, double> weight) | |
{ | |
double weightedValueSum = records.Sum(x => value(x) * weight(x)); | |
double weightSum = records.Sum(x => weight(x)); | |
if (weightSum != 0) | |
return weightedValueSum / weightSum; | |
else | |
throw new DivideByZeroException("Your message here"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Single pass implementation ->