Exemplo n.º 1
0
        static void Main(string[] args)
        {
            DynamicProgramming dynamicProgramming = new DynamicProgramming();

            /*  ProblemMedium.ArrayOfMultiples(7, 5);
             * ProblemMedium.ArrayOfMultiples(12, 10);
             * ProblemMedium.ArrayOfMultiples(17, 6);*/

            // ProblemMedium.FindTheWord("Diver", "I like to Diver Cards.");

            /*VariablesThings variables = new VariablesThings();
             * variables.AddListValuesSort(); // This adds all the values to all three array list. didn't want to messy up program class.
             * ProblemMedium.SortLargestArray(variables.NumberTest1,variables.NumberTest2,variables.NumberTest3);*/

            /* ProblemMedium.DiffercesOfTwoSquare(5);
             * ProblemMedium.DiffercesOfTwoSquare(6);
             * ProblemMedium.DiffercesOfTwoSquare(7);*/

            /*ProblemMedium.CenteredPentagonal(1);
            *  ProblemMedium.CenteredPentagonal(2);
            *  ProblemMedium.CenteredPentagonal(3);
            *  ProblemMedium.CenteredPentagonal(8);*/

            //  ConsoleExtras.ColoredText("Fibonacci",ConsoleColor.Cyan);
            //  Console.WriteLine( dynamicProgramming.FibonacciSequence(12));

            //  Console.WriteLine(dynamicProgramming.FibonacciSequenceMemoizion(200));

            //Console.WriteLine(dynamicProgramming.GridTravler(18, 18));
            //  Dictionary<string, ulong> gridDictionary = new Dictionary<string, ulong>();
            //  Console.WriteLine(dynamicProgramming.GridTravlerMemoize(100, 100,gridDictionary));


            //ApplyedMath applyedMath = new ApplyedMath();

            //applyedMath.DivideComplexNumbers("6+3i/7-5i");
            //applyedMath.DivideComplexNumbers("-32+8i/5+3i");
            //applyedMath.DivideComplexNumbers("25+19i/5-3i");
            //applyedMath.DivideComplexNumbers("-23+11i/5+1i");
            //applyedMath.DivideComplexNumbers("2-8i/3+5i");
            //applyedMath.DivideComplexNumbers("2-16i / - 3 - 1i");

            // Remove Duplicates from Sorted Array

            /*  int[] DuplicatedArray = { 4, 4, 4, 5, 5, 8, 8, 10,10, 22 };
             *
             * int len = ProblemMedium.RemoveDuplicates(DuplicatedArray);
             * for (int i = 0; i < len; i++)
             * {
             *    Console.Write(DuplicatedArray[i] +", ");
             * }
             */


            int[] numList = { 100, 99, 89, 72, 54, 52, 34, 30, 16, 15, 14, 10, 5, 4, 2 };
            //int[] numList = { 20, 20, 40, 44, 48, 50, 54 };
            int target = 42;

            Console.WriteLine("Array of numbers");

            Console.Write("Where is ");
            ConsoleExtras.ColoredWrite(target.ToString(), ConsoleColor.Green);
            Console.Write(" located in the array.\n");
            // Print array.
            for (int i = 0; i < numList.Length; i++)
            {
                if (i == numList.Length - 1)
                {
                    ConsoleExtras.ColoredWrite($"{numList[i]}\n", ConsoleColor.DarkYellow);
                    break;
                }
                ConsoleExtras.ColoredWrite($"{numList[i]}, ", ConsoleColor.DarkYellow);
            }

            Console.WriteLine($"Index: {DynamicProgramming.BinarySearch(numList,target)}");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Binary Search.
        /// It search for an index in a ordered list.
        /// </summary>
        /// <param name="Array"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public static int BinarySearch(int[] Array, int target)
        {
            bool leaseToGreater = false;

            // Checking to see is Array is order from lease to greatest.
            for (int i = 0; i < Array.Length; i++)
            {
                if (i + 1 > Array.Length - 1)
                {
                    break;
                }
                if (Array[i] == Array[i + 1])
                {
                    continue;
                }
                if (Array[i] < Array[i + 1])
                {
                    leaseToGreater = true;
                    break;
                }
                else
                {
                    leaseToGreater = false;
                    break;
                }
            }

            ConsoleExtras.DotLoadingAnimation("Checking Array a stortment", 200, 5); // DEBUGING
            int low = 0, high = Array.Length - 1, mid = 0;

            int steps = 1; // DEBUGING

            switch (leaseToGreater)
            {
            case true:
                ConsoleExtras.ColoredText("Array is storted from least to greatest."
                                          , ConsoleColor.Green); // DEBUGING
                while (low <= high)
                {
                    mid = (low + high) / 2;
                    #region Debuging
                    ConsoleExtras.ColoredWrite($"{steps}. ", ConsoleColor.Cyan);     // DEBUGING
                    for (int i = low; i < high + 1; i++)
                    {
                        if (i == high && i == low && Array[i] == target)
                        {
                            ConsoleExtras.ColoredWrite(Array[i].ToString(), ConsoleColor.Green);
                            break;
                        }
                        else if (i == high && i == low && Array[i] != target)
                        {
                            ConsoleExtras.ColoredWrite($"Array dose not contain {target}.", ConsoleColor.Red);
                            break;
                        }

                        if (i == high)
                        {
                            ConsoleExtras.ColoredWrite($"{Array[i]}", ConsoleColor.Blue);
                            break;
                        }
                        if (i == mid)
                        {
                            ConsoleExtras.ColoredWrite($"{Array[i]},", ConsoleColor.DarkRed);
                        }
                        else if (i == low)
                        {
                            ConsoleExtras.ColoredWrite($"{Array[i]}", ConsoleColor.Blue);
                            Console.Write(",");
                        }
                        else
                        {
                            Console.Write($"{Array[i]},");
                        }
                    }
                    Console.Write("\n");
                    steps++;            // DEBUGING
                    Thread.Sleep(1000); // DEBUGING
                    #endregion
                    if (Array[mid] == target)
                    {
                        return(mid);
                    }
                    else if (Array[mid] > target)
                    {
                        high = mid - 1;
                    }
                    else if (Array[mid] < target)
                    {
                        low = mid + 1;
                    }
                }
                break;

            case false:
                ConsoleExtras.ColoredText("Array is storted from greatest to least."
                                          , ConsoleColor.Cyan); // DEBUGING

                while (low <= high)
                {
                    mid = (low + high) / 2;
                    #region Debuging
                    ConsoleExtras.ColoredWrite($"{steps}. ", ConsoleColor.Cyan);     // DEBUGING
                    for (int i = low; i < high + 1; i++)
                    {
                        if (i == high && i == low && Array[i] == target)
                        {
                            ConsoleExtras.ColoredWrite(Array[i].ToString(), ConsoleColor.Green);
                            break;
                        }
                        else if (i == high && i == low && Array[i] != target)
                        {
                            ConsoleExtras.ColoredWrite($"Array dose not contain {target}.", ConsoleColor.Red);
                            break;
                        }

                        if (i == high)
                        {
                            ConsoleExtras.ColoredWrite($"{Array[i]}", ConsoleColor.Blue);
                            break;
                        }
                        if (i == mid)
                        {
                            ConsoleExtras.ColoredWrite($"{Array[i]},", ConsoleColor.DarkRed);
                        }
                        else if (i == low)
                        {
                            ConsoleExtras.ColoredWrite($"{Array[i]}", ConsoleColor.Blue);
                            Console.Write(",");
                        }
                        else
                        {
                            Console.Write($"{Array[i]},");
                        }
                    }
                    Console.Write("\n");
                    Thread.Sleep(1000); // DEBUGING
                    steps++;            // DEBUGING
                    #endregion
                    if (Array[mid] == target)
                    {
                        return(mid);
                    }
                    else if (Array[mid] < target)
                    {
                        high = mid - 1;
                    }
                    else if (Array[mid] > target)
                    {
                        low = mid + 1;
                    }
                }
                break;
            }
            return(-1);
        }