Skip to content

Instantly share code, notes, and snippets.

@acple
Created December 14, 2014 11:38
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 acple/e87f37d1d563c268992e to your computer and use it in GitHub Desktop.
Save acple/e87f37d1d563c268992e to your computer and use it in GitHub Desktop.
//http://msdn.microsoft.com/ja-jp/library/cc981895.aspx
public static class Extensions
{
public static double Median(this IEnumerable<double> source)
{
var count = source.Count();
if (count == 0) throw new InvalidOperationException("Cannot compute median for an empty set.");
return source.OrderBy(x => x).Take((count / 2) + 1).Skip((count - 1) / 2).Average();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment