public void FindMajorityElementByBoyerMooreAlgorithm_Should_Return_Null_if_no_majority_element_found_when_array_length_is_even()
        {
            int[] a      = new int[] { 1, 1, 3, 2, 1, 4 };
            var   result = ArrayOperations.FindMajorityElementByBoyerMooreAlgorithm(a);

            Assert.IsNull(result);
        }
        public void FindMajorityElementByBoyerMooreAlgorithm_Should_Return_Null_if_Array_size_is_Zero()
        {
            int[] a      = new int[] { };
            var   result = ArrayOperations.FindMajorityElementByBoyerMooreAlgorithm(a);

            Assert.IsNull(result);
        }
        public void FindMajorityElementByBruteForceMethod_Should_Return_Null_if_Array_size_is_Zero()
        {
            int[] a      = new int[] {  };
            var   result = ArrayOperations.FindMajorityElementByBruteForceMethod(a);

            Assert.IsNull(result);
        }
Exemplo n.º 4
0
        public void CopyTest()
        {
            Session session = new Session();

            foreach (int length in new[] { 24, 128 })
            {
                Tensor x = new Tensor(null, new[] { 1, 1, 1, length });
                x.Randomize(this.random);

                Tensor y1 = ArrayOperations.Copy(session, x);
                Helpers.AreTensorsEqual(x, y1);

                Tensor y2 = ArrayOperations.Copy(session, x);
                Helpers.AreTensorsEqual(x, y2);

                y1.RandomizeGradient(this.random);
                y2.RandomizeGradient(this.random);
                session.Unroll();

                Helpers.AreArraysEqual(
                    length,
                    y1.Gradient.Take(length).Zip(y2.Gradient, (a, b) => a + b).ToArray(),
                    x.Gradient);
            }
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            int    num2 = -15;
            string n1   = Convert.ToString(num2, 2);

            Console.WriteLine(n1);
            Stopwatch stopWatch = new Stopwatch();

            int[]  mas  = new int[10000];
            int[]  mas2 = new int[10000];
            Random rand = new Random();

            for (int i = 0; i < 10000; i++)
            {
                mas[i]  = rand.Next(1000);
                mas2[i] = rand.Next(1000);
            }
            stopWatch.Start();
            ArrayOperations.Quicksort(mas);
            stopWatch.Stop();
            TimeSpan quickSortTime = stopWatch.Elapsed;

            stopWatch.Restart();
            stopWatch.Start();
            ArrayOperations.MergeSort(mas2);
            stopWatch.Stop();
            TimeSpan mergeSortTime = stopWatch.Elapsed;

            Console.WriteLine("Quick sort time " + quickSortTime.TotalMilliseconds + " milliseconds");
            Console.WriteLine("Merge sort time " + mergeSortTime.TotalMilliseconds + " milliseconds");
            Console.ReadKey();
        }
Exemplo n.º 6
0
 // Used for CopyTo
 public QueuedArrayOperation(ArrayOperations operation, int index1, int index2, Text codeLine)
 {
     Operation = operation;
     Index1    = index1;
     Index2    = index2;
     CodeLine  = codeLine;
 }
Exemplo n.º 7
0
        /// <summary>
        /// https://www.ecma-international.org/ecma-262/6.0/#sec-string.raw
        /// </summary>
        private JsValue Raw(JsValue thisObj, JsValue[] arguments)
        {
            var cooked = TypeConverter.ToObject(_realm, arguments.At(0));
            var raw    = TypeConverter.ToObject(_realm, cooked.Get(JintTaggedTemplateExpression.PropertyRaw, cooked));

            var operations = ArrayOperations.For(raw);
            var length     = operations.GetLength();

            if (length <= 0)
            {
                return(JsString.Empty);
            }

            using var result = StringBuilderPool.Rent();
            for (var i = 0; i < length; i++)
            {
                if (i > 0)
                {
                    if (i < arguments.Length && !arguments[i].IsUndefined())
                    {
                        result.Builder.Append(TypeConverter.ToString(arguments[i]));
                    }
                }

                result.Builder.Append(TypeConverter.ToString(operations.Get((ulong)i)));
            }

            return(result.ToString());
        }
        public void WhenArrayHasZeroElementReturnWithZero()
        {
            int[] array  = new int[0];
            int   result = ArrayOperations.Sum(array);

            Assert.AreEqual(0, result);
        }
        public void WhenArrayHasZeroElementReturnWithEmptyList()
        {
            int[]      array  = new int[0];
            List <int> result = ArrayOperations.EvenNumbers(array);

            Assert.AreEqual(0, result.Count);
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            //Task1
            int[] intArray1 = { 1, 2, 3, 4, 3, 2, 1 };
            int[] intArray2 = { 1, 100, 50, -51, 1, 1 };
            int[] intArray3 = { 1, 2, 3, 4, 5, 6 };
            int[] intArray4 = { 0, 0, 0, 0, 0 };

            Console.WriteLine(ArrayOperations.FindTheCentral(intArray1));
            Console.WriteLine(ArrayOperations.FindTheCentral(intArray2));
            Console.WriteLine(ArrayOperations.FindTheCentral(intArray3));
            Console.WriteLine(ArrayOperations.FindTheCentral(intArray4));
            Console.WriteLine();

            //Task2
            string firstString  = "xyaabBdcccdefww";
            string secondString = "xxxxyyyyabklmopq";
            string thirdString  = "abcdefghijklmnopqrstuvwxyz";

            string firstPlusSecond = StringOperations.Longest(firstString, secondString);

            thirdString = StringOperations.Longest(thirdString, thirdString);

            Console.WriteLine(firstPlusSecond);
            Console.WriteLine(thirdString);
            Console.WriteLine();

            //Task3
            int result = BitOperations.Insertion(8, 15, 0, 0);

            Console.WriteLine(result);
            result = BitOperations.Insertion(0, 15, 30, 30);
            Console.WriteLine(result);
            result = BitOperations.Insertion(0, 15, 0, 30);
            Console.WriteLine(result);
            result = BitOperations.Insertion(15, -15, 0, 4);
            Console.WriteLine(result);
            result = BitOperations.Insertion(15, int.MaxValue, 3, 5);
            Console.WriteLine(result);

            /*[TestCase(int.MaxValue, int.MaxValue, 3, 5, ExpectedResult = int.MaxValue)]
             * [TestCase(15, int.MaxValue, 3, 5, ExpectedResult = 63)]
             * [TestCase(15, 15, 1, 3, ExpectedResult = 15)]
             * [TestCase(15, 15, 1, 4, ExpectedResult = 31)]
             * [TestCase(15, -15, 0, 4, ExpectedResult = 31)]
             * [TestCase(15, -15, 1, 4, ExpectedResult = 15)]
             * [TestCase(-8, -15, 1, 4, ExpectedResult = -6)]*/


            Console.WriteLine(BitOperations.Insertion(int.MaxValue, int.MaxValue, 3, 5));
            Console.WriteLine(BitOperations.Insertion(15, int.MaxValue, 3, 5));
            Console.WriteLine(BitOperations.Insertion(15, 15, 1, 3));
            Console.WriteLine(BitOperations.Insertion(15, 15, 1, 4));
            Console.WriteLine(BitOperations.Insertion(15, -15, 0, 4));
            Console.WriteLine(BitOperations.Insertion(15, -15, 1, 4));
            Console.WriteLine(BitOperations.Insertion(-8, -15, 1, 4));


            Console.ReadLine();
        }
        public void WhenArrayHasZeroElementReturnWithZeroAverage()
        {
            int[] array  = new int[0];
            float result = ArrayOperations.Average(array);

            Assert.AreEqual(0, result);
        }
        public void FindMajorityElementByBruteForceMethod_Should_Return_Null_if_no_majority_element_found_when_array_length_is_odd()
        {
            int[] a      = new int[] { 1, 1, 3, 2, 1, 4, 5 };
            var   result = ArrayOperations.FindMajorityElementByBruteForceMethod(a);

            Assert.IsNull(result);
        }
Exemplo n.º 13
0
        private static void DemoMaxSumSubArrayByKadanesAlgorithm()
        {
            //var inputArray = new int[] { 100, 15, 20, -5, 0, 101 };
            //var inputArray = new int[] { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
            var inputArray = new int[] { -4, 2, 1, -5, 2, 1 };


            Print(inputArray, "All elements in the array: ");
            var result = ArrayOperations.FindMaxSumSubArrayByKadanesAlgorithm(inputArray);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(); Console.WriteLine();
            Console.WriteLine($"Highest sum: {result.Sum}");

            for (int si = 0; si < result.SubArrayIndexes.Count; si++)
            {
                Console.Write($"Sub-array #{si + 1} is [{result.SubArrayIndexes[si].Item1} to {result.SubArrayIndexes[si].Item2}] with length {result.SubArrayIndexes[si].Item2 - result.SubArrayIndexes[si].Item1 + 1} : ");
                for (int i = result.SubArrayIndexes[si].Item1; i <= result.SubArrayIndexes[si].Item2; i++)
                {
                    Console.Write(inputArray[i].ToString());

                    if (i + 1 <= result.SubArrayIndexes[si].Item2)
                    {
                        Console.Write(" ,");
                    }
                }
                Console.WriteLine();
            }
        }
Exemplo n.º 14
0
        static void Main(string[] args)
        {
            #region Objects
            ArrayOperations operationsObj = new ArrayOperations();
            #endregion

            #region DetectDuplicates
            Console.WriteLine("----Search for Duplicates----");
            operationsObj.DetectDuplicates("SAMANTHA");
            #endregion

            #region ReverseArray

            #region StandardWay
            int[] param = new int[] { 1, 3, 4, 6 };
            operationsObj.ReverseArray(param, 0, 3);
            #endregion

            #region WorseCase

            int[] inputArr = new int[] { 1, 3, 4, 6, 8 };
            operationsObj.WorseCaseReverseArray(inputArr);
            #endregion

            #region RecursiveWay
            operationsObj.RecursiveReverseArray(inputArr, 0, 4);
            #endregion

            #endregion

            Console.Read();
        }
        public void WhenArrayHasOneEvenAndOneOddElementReturnWithTheEvenOne()
        {
            int[] array = new int[] { 6, 7 };

            List <int> result = ArrayOperations.EvenNumbers(array);

            Assert.AreEqual(6, result[0]);
        }
        public void WhenArrayHasOneEvenElementReturnWithTheValueOfElement()
        {
            int[] array = new int[] { 6 };

            List <int> result = ArrayOperations.EvenNumbers(array);

            Assert.AreEqual(6, result[0]);
        }
        public void WhenArrayHasOneElementReturnWithTheExactValueOfElement()
        {
            int[] array = new int[] { 5 };

            float result = ArrayOperations.Average(array);

            Assert.AreEqual(5, result);
        }
        public void WhenArrayHasNegativeElements()
        {
            int[] array = new int[] { -5, 6, 7 };

            int result = ArrayOperations.Sum(array);

            Assert.AreEqual(8, result);
        }
        public void WhenArrayHasOneElementReturnWithTheValueOfElement()
        {
            int[] array = new int[] { 5 };

            int result = ArrayOperations.Sum(array);

            Assert.AreEqual(5, result);
        }
        public void WhenArrayHasTwoEvenAndOneOddElementReturnWithTheAverageOfEvenOnes()
        {
            int[] array = new int[] { 6, 7, 8 };

            float result = ArrayOperations.AverageOfEvenNumbers(array);

            Assert.AreEqual(7, result);
        }
        public void FindMaxProductSubArrayByBruteForceMethod_should_return_null_when_a_Array_of_0X0()
        {
            var inputToTest = new int[] { };

            var result = ArrayOperations.FindMaxProductSubArrayByBruteForceMethod(inputToTest);

            Assert.IsNull(result);
        }
 public void MergeSort_NormalArray_PositiveTest(int[] a)
 {
     int[] tmparr = new int[a.Length];
     Array.Copy(a, tmparr, a.Length);
     Array.Sort(tmparr);
     ArrayOperations.MergeSort(a);
     Assert.AreEqual(tmparr, a);
 }
        public void WhenArrayHasMoreElementsReturnWithTheSumOfElements()
        {
            int[] array = new int[] { 5, 6, 7 };

            int result = ArrayOperations.Sum(array);

            Assert.AreEqual(18, result);
        }
        public void FindMajorityElementByBruteForceMethod_Should_Return_MajorityElement_if_Array_size_is_one()
        {
            int[] a      = new int[] { 5 };
            var   result = ArrayOperations.FindMajorityElementByBruteForceMethod(a);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count == 1);
            Assert.IsTrue(result.MajorityElement == 5);
        }
        public void FindMajorityElementByBruteForceMethod_Should_Return_Majority_Element()
        {
            int[] a      = new int[] { 1, 1, 3, 2, 1 };
            var   result = ArrayOperations.FindMajorityElementByBoyerMooreAlgorithm(a);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count == 3);
            Assert.IsTrue(result.MajorityElement == 1);
        }
Exemplo n.º 26
0
        public void SetToRightLargestNumber(int[] numbers, int[] expectedResult)
        {
            int[] result = ArrayOperations.SetToRightLargestNumber(numbers);

            for (int i = 0; i < numbers.Length; i++)
            {
                expectedResult[i].ShouldBe(result[i]);
            }
        }
Exemplo n.º 27
0
        public void FindMaxSumSubArrayUsingKadanesAlgorithm_Should_Find_MaxSumSubArray_In_An_Array_With_Positive_elements_Sorted_Desc()
        {
            int[] a      = { 2, 4, 5, 7 };
            var   result = ArrayOperations.FindMaxSumSubArrayByKadanesAlgorithm(a);

            Assert.IsTrue(result.Sum == 18);
            Assert.IsTrue(result.SubArrayIndexes.Count == 1);
            Assert.IsTrue(result.SubArrayIndexes[0].Item1 == 0);
            Assert.IsTrue(result.SubArrayIndexes[0].Item2 == a.Length - 1);
        }
Exemplo n.º 28
0
        public void FindMaxSumSubArrayUsingKadanesAlgorithm_Should_Find_MaxSumSubArray_In_An_Array_With_Negative_elements_Sorted_Asc()
        {
            int[] a      = { -7, -5, -3, -1 };
            var   result = ArrayOperations.FindMaxSumSubArrayByKadanesAlgorithm(a);

            Assert.IsTrue(result.Sum == a[3]);
            Assert.IsTrue(result.SubArrayIndexes.Count == 1);
            Assert.IsTrue(result.SubArrayIndexes[0].Item1 == 3);
            Assert.IsTrue(result.SubArrayIndexes[0].Item2 == 3);
        }
Exemplo n.º 29
0
        public void FindMaxSumSubArrayUsingKadanesAlgorithm_Should_Find_MaxSumSubArray_In_A_SingleElementArray()
        {
            int[] a      = { -2 };
            var   result = ArrayOperations.FindMaxSumSubArrayByKadanesAlgorithm(a);

            Assert.IsTrue(result.Sum == a[0]);
            Assert.IsTrue(result.SubArrayIndexes.Count == 1);
            Assert.IsTrue(result.SubArrayIndexes[0].Item1 == 0);
            Assert.IsTrue(result.SubArrayIndexes[0].Item2 == 0);
        }
Exemplo n.º 30
0
        public void FindMaxSumSubArrayUsingKadanesAlgorithm_Should_Find_Whole_Array_AsResult_If_Whole_Array_sum_Greater_Than_Sub_Array_Sum()
        {
            int[] a      = { 7, 5, 3, 1, -1, 5, 2 };
            var   result = ArrayOperations.FindMaxSumSubArrayByKadanesAlgorithm(a);

            Assert.IsTrue(result.Sum == 22);
            Assert.IsTrue(result.SubArrayIndexes.Count == 1);
            Assert.IsTrue(result.SubArrayIndexes[0].Item1 == 0);
            Assert.IsTrue(result.SubArrayIndexes[0].Item2 == a.Length - 1);
        }