public static double Mean <T>(this SumList <T> list)
        {
            if (list == null || list.Count == 0)
            {
                return(0.0);
            }

            return(list.Sum() / list.Count);
        }
        public static T Mode <T>(this SumList <T> list)
        {
            if (list == null || list.Count == 0)
            {
                return(default(T));
            }

            return((from item in list
                    group item by item into grouped
                    orderby grouped.Count() descending
                    select grouped.Key).First());
        }
Beispiel #3
0
 public SumListEnumerator(SumList <T> list)
 {
     _items = list;
 }
Beispiel #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("===============================");
            Console.WriteLine("         SUMLIST TESTS         ");
            Console.WriteLine("===============================");

            SumList <int>    a = new SumList <int>();
            SumList <int>    b = new SumList <int>(new int[] { 1, 2, 3, 4 });
            SumList <double> e = new SumList <double>(4);

            a.Add(1);
            a.Add(2);
            a.Add(3);
            a.Add(4);
            e.Add(1.1);
            e.Add(2.2);
            e.Add(3.3);
            e.Add(4.4);

            Console.WriteLine("List: {0}", a);
            Console.WriteLine("Size = {0}", a.Capacity);
            Console.WriteLine("Avg = {0}", a.Count);
            Console.WriteLine("Sum = {0}", a.Sum());
            Console.WriteLine("Mean = {0}", a.Mean <int>());
            Console.WriteLine("Mode = {0}", a.Mode <int>());
            Console.WriteLine("3rd element = {0}", a[2]);
            Console.WriteLine("Cast to integer: {0}", (int)a);

            if (a.Equals(b))
            {
                Console.WriteLine("a and b are equal");
            }
            else
            {
                Console.WriteLine("a and b are NOT equal");
            }

            foreach (object i in a)
            {
                foreach (object j in a)
                {
                    Console.Write("{0}-{1},", i, j);
                }
                Console.WriteLine("");
            }

            Console.WriteLine("\r\nNon-numeric contents:\r\n");
            var list = new SumList <string> {
                "one fish", "two fish", "red fish", "two fish", "blue fish"
            };

            Console.WriteLine("Capacity = {0}\nCount = {1}\nSum = {2}", list.Capacity, list.Count, list.Sum());
            Console.WriteLine("Mean = {0}\nMode = {1}", list.Mean(), list.Mode());
            Console.WriteLine(list);


            Console.WriteLine("\r\n");
            Console.WriteLine("===============================");
            Console.WriteLine("      SORTEDSUMLIST TESTS      ");
            Console.WriteLine("===============================");

            var descendingNumbers = Enumerable.Range(1, 10).OrderByDescending(x => x);
            var sortedList        = new SortedSumList <int>(descendingNumbers);

            Console.WriteLine("Capacity = {0}\nCount = {1}\nSum = {2}", sortedList.Capacity, sortedList.Count, sortedList.Sum());
            Console.WriteLine("Mean = {0}\nMode = {1}\nMedian = {2}", sortedList.Mean(), sortedList.Mode(), sortedList.Median());
            Console.WriteLine(sortedList);
            Console.WriteLine("\r\n");

            foreach (var n in descendingNumbers)
            {
                sortedList.Add(n);
                sortedList.Add(5);
            }

            Console.WriteLine("Capacity = {0}\nCount = {1}\nSum = {2}", sortedList.Capacity, sortedList.Count, sortedList.Sum());
            Console.WriteLine("Mean = {0}\nMode = {1}\nMedian = {2}", sortedList.Mean(), sortedList.Mode(), sortedList.Median());
            Console.WriteLine(sortedList);

            // Compare SumList and SumSortedList
            SumList <int>       d = new SumList <int>(new int[] { 4, 3, 2, 1 });
            SortedSumList <int> c = new SortedSumList <int>(new int[] { 3, 4, 2, 1 });

            if (d == c)
            {
                Console.WriteLine("The two lists are equal");  // This
            }
            else
            {
                Console.WriteLine("The two lists are NOT equal");
            }

            d.Add(0);

            if (c == d)
            {
                Console.WriteLine("The two lists are equal");
            }
            else
            {
                Console.WriteLine("The two lists are NOT equal");  // This
            }
            Console.ReadLine();
        }