Beispiel #1
0
        public void MaxMinElementWorks()
        {
            var list = new List <TestObject>()
            {
                new TestObject()
                {
                    StringProperty = "a",
                    IntProperty    = 1
                },
                new TestObject()
                {
                    StringProperty = "a",
                    IntProperty    = 3
                },
                new TestObject()
                {
                    StringProperty = "a",
                    IntProperty    = -1
                },
                new TestObject()
                {
                    StringProperty = "b",
                    IntProperty    = 2
                }
            };

            list.MaxElement(x => x.IntProperty).Should().Be(list[1]);
            list.MinElement(x => x.IntProperty).Should().Be(list[2]);
        }
Beispiel #2
0
        public void MinMaxElement()
        {
            List <Tuple <int, double> > data = new List <Tuple <int, double> >();

            Assert.AreEqual(null, data.MinElement());
            Assert.AreEqual(null, data.MaxElement());

            data.Add(Tuple.Create(1, 2.3));
            Assert.AreEqual(data[0], data.MinElement());
            Assert.AreEqual(data[0], data.MaxElement());

            data.Add(Tuple.Create(6, 1.3));
            Assert.AreEqual(data[0], data.MinElement());
            Assert.AreEqual(data[1], data.MaxElement());

            Assert.AreEqual(data[1], data.MinElement(p => p.Item2));
            Assert.AreEqual(data[0], data.MaxElement(p => p.Item2));
        }
        static void Main()
        {
            IEnumerable<int> list = new List<int>() { 5, 6, 7, 8, 3, 4, 6, 1 };

            int sum = list.SumOfCollection();
            int product = list.ProductOfCollection();
            int min = list.MinElement();
            int max = list.MaxElement();
            int average = list.AverageOfCollection();

            Console.WriteLine(average);
        }
Beispiel #4
0
        public void MaxElemTests()
        {
            List <int> someList = new List <int>()
            {
                3, 2, 1, 6, 5, 4
            };

            var actualMaxElem    = someList.MaxElement(x => - x);
            var expecetedMaxElem = 1;

            Assert.AreEqual(expecetedMaxElem, actualMaxElem);
        }
        public List <IUnitCommand> GetCommandsForUnit(IUnit unit, InfluenceMaps maps)
        {
            IBarbarianGoalBrain brainToExecute = GoalBrains.MaxElement(brain => brain.GetUtilityForUnit(unit, maps));

            if (brainToExecute != null)
            {
                return(brainToExecute.GetCommandsForUnit(unit, maps));
            }
            else
            {
                return(new List <IUnitCommand>());
            }
        }
        static void Main()
        {
            IEnumerable <int> list = new List <int>()
            {
                5, 6, 7, 8, 3, 4, 6, 1
            };

            int sum     = list.SumOfCollection();
            int product = list.ProductOfCollection();
            int min     = list.MinElement();
            int max     = list.MaxElement();
            int average = list.AverageOfCollection();

            Console.WriteLine(average);
        }
        public static void Main()
        {
            List<int> nums = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            var filteredCollect = nums.WhereNot(num => num % 2 == 0);

            Console.WriteLine(string.Join(", ", filteredCollect));

            var students = new List<Student>()
            {
                new Student("Pipo", 3.50),
                new Student("Miko", 2.99),
                new Student("Maq", 6.0)
            };

            Console.WriteLine(students.MaxElement(st => st.Grades));
        }
Beispiel #8
0
        public void MinMaxElement()
        {
            List <KeyValuePair <int, int> > list = new List <KeyValuePair <int, int> >
            {
                new KeyValuePair <int, int>(2, 3),
                new KeyValuePair <int, int>(5, 7),
                new KeyValuePair <int, int>(4, 6),
                new KeyValuePair <int, int>(-5, 4)
            };

            KeyValuePair <int, int> min = list.MinElement((kv) => kv.Value);

            Assert.AreEqual(2, min.Key);
            Assert.AreEqual(3, min.Value);

            KeyValuePair <int, int> max = list.MaxElement((kv) => kv.Key);

            Assert.AreEqual(5, max.Key);
            Assert.AreEqual(7, max.Value);
        }
Beispiel #9
0
        static void Main(string[] args)
        {
            /* Substring test*/
            StringBuilder str = new StringBuilder("abcdef");
            StringBuilder substringStr = str.Substring(0, 2);   // substring from 0 to 1
            Console.WriteLine(substringStr.ToString());
            Console.WriteLine("-------------------------");

            /* IEnumerable test */
            List<int> list = new List<int>
            {
                1, 3, 5, 7, 21
            };
            int sum = list.Sum<int>();
            Console.WriteLine("sum =  {0}", sum);
            int product = list.Product<int>();
            Console.WriteLine("product =  {0}", product);
            int max = list.MaxElement<int>();
            Console.WriteLine("max = {0}", max);
            int min = list.MinElement<int>();
            Console.WriteLine("min = {0}", min);    // TODO Fix min = 0
            int avg = list.Average<int>();
            Console.WriteLine("avg = {0}", avg);    // TODO Fix avg = 0
            Console.WriteLine("-------------------------");

            /* First before last test */
            Student[] arrayOfStudents = new Student[]
            {
                new Student("Pesho", "Toshev", 72),
                new Student("Pesho", "Goshev", 21),
                new Student("Pesheto", "Peshev", 19),
                new Student("Peneto", "Oshev", 17),
                new Student("Gosho", "Peshev", 32)
            };
            Student[] array = LINQMethods.FirstBeforeLastName(arrayOfStudents);
            foreach (var student in array)
            {
                Console.WriteLine(student.ToString());
            }
            Console.WriteLine("-------------------------");

            /* Age Range Test*/
            string[] students = LINQMethods.AgeRange(arrayOfStudents);
            foreach (var student in students)
            {
                Console.WriteLine(student);
            }
            Console.WriteLine("-------------------------");

            /* Order Student */
            array = LINQMethods.OrderStudents(arrayOfStudents);
            foreach (var student in array)
            {
                Console.WriteLine(student.ToString());
            }
            Console.WriteLine("-------------------------");

            /* Divisible by 7 and 3 */
            LINQMethods.PrintNumsDivisibleBy7And3(list.ToArray());
            Console.WriteLine("-------------------------");

            //  ////////////////////////////////////////  7 and 8

            /* Student groups */
            List<Student> listOfStudents = new List<Student>
            {
                new Student("Name5", "Namov", 34, "1211", "02000s", "*****@*****.**", new List<int> { 5, 6, 4 }, 2, new Group(1, "Math")),
                new Student("Name2", "Namov2", 3, "1211", "000s", "*****@*****.**", new List<int> { 5, 6, 4 }, 2, new Group(2, "Sport")),
                new Student("Name3", "Namov3", 4, "1211", "000s", "*****@*****.**", new List<int> { 5, 6, 4 }, 1, new Group(1, "Math")),
                new Student("Name4", "Namov4", 11, "1211", "02000s", "*****@*****.**", new List<int> { 5, 6, 4 }, 12, new Group(3, "Biology")),
                new Student("Name", "Namov5", 20, "1211", "000s", "*****@*****.**", new List<int> { 5, 6, 4 }, 2, new Group(1, "Math"))
            };
            Student[] newArrOfStudents = LINQMethods.StudentsGroup(listOfStudents);
            foreach (Student student in newArrOfStudents)
            {
                Console.WriteLine(student.ToStringAll());
            }
            Console.WriteLine("-------------------------");

            //// 10

            /* Extract students by email */
            newArrOfStudents = LINQMethods.ExtractStudentsByEmail(listOfStudents);
            foreach (Student student in newArrOfStudents)
            {
                Console.WriteLine(student.ToStringAll());
            }
            Console.WriteLine("-------------------------");

            /* Extract students by phone */
            newArrOfStudents = LINQMethods.ExtractStudentsByPhone(listOfStudents);
            foreach (Student student in newArrOfStudents)
            {
                Console.WriteLine(student.ToStringAll());
            }
            Console.WriteLine("-------------------------");

            //////// 14, 15

            /* Infinite Convergent Series */
            double precision = 0.001;

            Console.WriteLine("1 + 1/2 + 1/4 + 1/8 + 1/16 + ... = {0:f2}", InfiniteConvergentSeries.ConvergentSum(i => 1.0 / Math.Pow(2, i), precision));
            Console.WriteLine("1 + 1/2! + 1/3! + 1/4! + 1/5! + ... = {0:f2}", InfiniteConvergentSeries.ConvergentSum(i => 1.0 / InfiniteConvergentSeries
                                                                                                                                        .Factorial(i + 1), precision));
            Console.WriteLine("1 + 1/2 - 1/4 + 1/8 - 1/16 + ... = {0:f2}", InfiniteConvergentSeries.ConvergentSum(i => i == 0 ? 1 : -1.0 / Math.Pow(-2, i), precision));
            Console.WriteLine("-------------------------");

            /* Timer */
            TimerDelegate timerDelegate = new TimerDelegate(1, test);
            Console.WriteLine("-------------------------");
        }
Beispiel #10
0
        private void Analyze()
        {
            _Descriptions = new Dictionary <string, Tuple <int, float> >();
            for (int ii = 0; ii < _Transactions.Count; ++ii)
            {
                var transaction = _Transactions[ii];
                if (!_Categories.Contains(transaction.Category) && transaction.Category != "Payment")
                {
                    _Categories.Add(transaction.Category);
                }

                string desc = transaction.Description.ToLower();
                //Try categorize based on keywords
                if (transaction.Category == "Unknown")
                {
                    foreach (string keyword in _Keywords.Keys)
                    {
                        if (desc.Contains(keyword.ToLower()))
                        {
                            transaction.Category = _Keywords[keyword];
                        }
                    }
                }

                //Count expenses based on equal keywords
                Tuple <int, float> descData;
                if (_Descriptions.TryGetValue(desc, out descData))
                {
                    _Descriptions[desc] = new Tuple <int, float>(descData.Item1 + 1, descData.Item2 + transaction.Amount);
                }
                else
                {
                    _Descriptions.Add(desc, new Tuple <int, float>(1, transaction.Amount));
                }
            }

            Dictionary <string, float> categoryExpense = _Categories.ToDictionary(c => c, c => 0.0f);

            foreach (Transaction t in _Transactions)
            {
                if (_Categories.Contains(t.Category))
                {
                    categoryExpense[t.Category] += t.Amount;
                }
            }

            _Chart.Series[0].Points.Clear();
            foreach (var kvp in categoryExpense)
            {
                _Chart.Series[0].Points.AddXY(string.Format("{0} ({1})", kvp.Key, kvp.Value.ToString("0.00")), kvp.Value);
            }

            tbRecurrents.Text = string.Join(Environment.NewLine,
                                            _Descriptions
                                            .OrderByDescending(kvp => kvp.Value)
                                            .Take(10)
                                            .Select(kvp => string.Format("{0} {1} {2}",
                                                                         kvp.Key.PadRight(35),
                                                                         kvp.Value.Item1.ToString().PadRight(6),
                                                                         kvp.Value.Item2.ToString("0.00"))));

            List <Transaction> expenses = _Transactions.Where(t => t.Amount > 0).ToList();

            float total   = expenses.Sum(t => t.Amount);
            float average = expenses.Average(t => t.Amount);

            Trace.Assert(Math.Abs(total / expenses.Count - average) < 0.001);

            tbAverageExpense.Text = average.ToString("0.00");

            List <float> dailyExpense = new List <float>();
            DateTime     currentDate  = _Transactions.First().Date.AddDays(-1);

            foreach (Transaction t in expenses)
            {
                if (t.Date > currentDate)
                {
                    dailyExpense.Add(t.Amount);
                    currentDate = t.Date;
                }
                else if (t.Date == currentDate)
                {
                    dailyExpense[dailyExpense.Count - 1] += t.Amount;
                }
            }

            Trace.Assert(Math.Abs(total - dailyExpense.Sum()) < 0.001);

            tbDailyAverage.Text = dailyExpense.Average().ToString("0.00");

            Transaction max = expenses.MaxElement(t => t.Amount);
            Transaction min = expenses.MinElement(t => t.Amount);

            tbMax.Text = string.Format("{0} {1}", max.Description.PadRight(20), max.Amount.ToString("0.00"));
            tbMin.Text = string.Format("{0} {1}", min.Description.PadRight(20), min.Amount.ToString("0.00"));

            _BeginDate = _Transactions.First().Date;
            _EndDate   = _Transactions.Last().Date;

            tbExpenseBegin.Tag = _BeginDate;
            tbExpenseEnd.Tag   = _EndDate;
            UpdateRangeTextBoxesFromTag();
        }