public double CalculateAverage(List <double> values, AveragingMethod averagingMethod) { if (values == null) { throw new ArgumentNullException(nameof(values)); } switch (averagingMethod) { case AveragingMethod.Mean: return(values.Sum() / values.Count); case AveragingMethod.Median: var sortedValues = values.OrderBy(x => x).ToList(); int n = sortedValues.Count; if (n % 2 == 1) { return(sortedValues[(n - 1) / 2]); } return((sortedValues[sortedValues.Count / 2 - 1] + sortedValues[n / 2]) / 2); default: throw new ArgumentException("Invalid averagingMethod value"); } }
public double CalculateAverage(List <double> values, AveragingMethod averagingMethod) { if (values == null) { throw new ArgumentNullException(nameof(values)); } if (averagingMethod == null) { throw new ArgumentNullException(nameof(averagingMethod)); } return(averagingMethod(values)); }
public double CalculateAverage(List <double> values, AveragingMethod averagingMethod) { if (values == null) { throw new ArgumentNullException(nameof(values)); } switch (averagingMethod) { case AveragingMethod.Mean: return(CalculateMean(values)); case AveragingMethod.Median: return(CalculateMedian(values)); default: throw new ArgumentException("Invalid averagingMethod value"); } }
public double CalculateAverage(List <double> values, AveragingMethod averagingMethod) { switch (averagingMethod) { case AveragingMethod.Mean: return(values.Sum() / values.Count); case AveragingMethod.Median: var sortedValues = values.OrderBy(x => x).ToList(); if (sortedValues.Count % 2 == 1) { return(sortedValues[(sortedValues.Count - 1) / 2]); } return((sortedValues[(sortedValues.Count / 2) - 1] + sortedValues[(sortedValues.Count / 2)]) / 2); default: throw new ArgumentException("Invalid averagingMethod value"); } }
public void AverageCalculatorTests_ValidEnum(AveragingMethod method, double result) { Calculator calculator = new Calculator(); Assert.AreEqual(result, calculator.CalculateAverageWithEnum(values, method), 0.000001); }
public Mathematician(AveragingMethod averagingMethod) { this.averagingMethod = averagingMethod; }