using System;
using System.Collections.Generic;
using System.Linq;

namespace InterpreterLibrary.Models
{
    internal static class MiscExtensions
    {
        public static double Median<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector)
        {
            var lst = source.OrderBy(selector).ToList();
            var half = lst.Count / 2;
            if (lst.Count % 2 == 0)
            {
                return (selector(lst[half - 1]) + selector(lst[half])) / 2.0;
            }
            return selector(lst[half]);
        }
        public static double MedianOfLargerHalf<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector)
        {
            var lst = source.OrderBy(selector).ToList();
            return lst.Skip(lst.Count / 2).Median(selector);
        }
    }

}

