예제 #1
0
        public static void LinkedListProblems()
        {
            // Linked List

            // Reverse a linked list
            LinkedList <int> linked = new LinkedList <int>();

            linked.AddLast(1);
            linked.AddLast(2);
            linked.AddLast(3);
            LinkedList <int> linked2 = new LinkedList <int>();

            linked2.AddLast(1);
            linked2.AddLast(2);
            linked2.AddLast(3);

            PrintHelpers.PrintArray("Reverse Linked List", linked2.ToArray(), ReverseLinkedList.Reverse(linked).ToArray());

            var customLinked = new CustomLinkedList <int>();

            customLinked.Head = new Node <int> {
                Value = 1
            };
            customLinked.Head.Next = new Node <int> {
                Value = 2
            };
            customLinked.Head.Next.Next = new Node <int> {
                Value = 2
            };
            customLinked.Head.Next.Next.Next = new Node <int> {
                Value = 3
            };
            var customLinked2 = new CustomLinkedList <int>();

            customLinked2.Head = new Node <int> {
                Value = 1
            };
            customLinked2.Head.Next = new Node <int> {
                Value = 2
            };
            customLinked2.Head.Next.Next = new Node <int> {
                Value = 2
            };
            customLinked2.Head.Next.Next.Next = new Node <int> {
                Value = 3
            };

            PrintHelpers.PrintArray("Reverse a Custom Linked List", customLinked2, new CustomLinkedList <int>
            {
                Head = ReverseLinkedList.Reverse(customLinked)
            });

            // Remove duplicates in a linked list
            linked = new LinkedList <int>();
            linked.AddLast(1);
            linked.AddLast(2);
            linked.AddLast(2);
            linked.AddLast(3);
            linked2 = new LinkedList <int>();
            linked2.AddLast(1);
            linked2.AddLast(2);
            linked2.AddLast(2);
            linked2.AddLast(3);
            PrintHelpers.PrintArray("Remove Duplicates", linked2.ToArray(), RemoveDuplicates.RemoveDuplicate(linked).ToArray());

            // Remove duplicates in a Custom linked list
            customLinked      = new CustomLinkedList <int>();
            customLinked.Head = new Node <int> {
                Value = 1
            };
            customLinked.Head.Next = new Node <int> {
                Value = 2
            };
            customLinked.Head.Next.Next = new Node <int> {
                Value = 2
            };
            customLinked.Head.Next.Next.Next = new Node <int> {
                Value = 3
            };
            customLinked2      = new CustomLinkedList <int>();
            customLinked2.Head = new Node <int> {
                Value = 1
            };
            customLinked2.Head.Next = new Node <int> {
                Value = 2
            };
            customLinked2.Head.Next.Next = new Node <int> {
                Value = 2
            };
            customLinked2.Head.Next.Next.Next = new Node <int> {
                Value = 3
            };
            PrintHelpers.PrintArray("Remove Duplicates Custom Linked List", customLinked2, RemoveDuplicates.RemoveDuplicate(customLinked));


            // Remove Node with given key in a linked list
            PrintHelpers.PrintArray("Remove Node with given key ", linked2.ToArray(), RemoveNodeWithGivenKey.Remove(linked, 2).ToArray());

            // Remove Node with given key in a Custom linked list
            PrintHelpers.PrintArray("Remove Node with given key  Custom Linked List", customLinked2, RemoveNodeWithGivenKey.Remove(customLinked, 2));


            linked = new LinkedList <int>();
            linked.AddLast(1);
            linked.AddLast(2);
            linked.AddLast(2);
            linked.AddLast(3);
            linked2 = new LinkedList <int>();
            linked2.AddLast(1);
            linked2.AddLast(2);
            linked2.AddLast(2);
            linked2.AddLast(3);

            customLinked      = new CustomLinkedList <int>();
            customLinked.Head = new Node <int> {
                Value = 1
            };
            customLinked.Head.Next = new Node <int> {
                Value = 2
            };
            customLinked.Head.Next.Next = new Node <int> {
                Value = 2
            };
            customLinked.Head.Next.Next.Next = new Node <int> {
                Value = 3
            };
            customLinked2      = new CustomLinkedList <int>();
            customLinked2.Head = new Node <int> {
                Value = 1
            };
            customLinked2.Head.Next = new Node <int> {
                Value = 2
            };
            customLinked2.Head.Next.Next = new Node <int> {
                Value = 2
            };
            customLinked2.Head.Next.Next.Next = new Node <int> {
                Value = 3
            };
            // N from last Node in a linked list
            PrintHelpers.PrintArray("N from last Node in a linked list", linked2.ToArray(), new LinkedList <int>(new[] { NFromLastNode.GetNodeBasedOnPosition(linked, 2).Value }).ToArray(), "Node: 2");

            // N from last Node in a Custom linked list
            PrintHelpers.PrintArray("N from last Node in a custom linked list", customLinked2, new CustomLinkedList <int>
            {
                Head = new Node <int> {
                    Value = NFromLastNode.GetNodeBasedOnPosition(customLinked, 2).Value
                }
            }, "Node: 2");

            // Swap N Node with Head
            PrintHelpers.PrintArray("Swap N Node with Head", linked2.ToArray(), SwapNnodeWithHead.SwapBasedOnPosition(linked, 2).ToArray(), "Node: 2");
            PrintHelpers.PrintArray("Swap N Node with Head", customLinked2, SwapNnodeWithHead.SwapBasedOnPosition(customLinked, 2), "Node: 2");


            // Intersection Point of 2 Linked list
            //LinkedListNode<int> node = new LinkedListNode<int>(5);
            //linked = new LinkedList<int>();
            //linked.AddLast(1);
            //linked.AddLast(2);
            //linked.AddLast(2);
            //linked.AddLast(node);
            //linked.AddLast(3);
            //linked2 = new LinkedList<int>();
            //linked2.AddLast(1);
            //linked2.AddLast(2);
            //linked2.AddLast(2);
            //linked2.AddLast(node);
            //linked2.AddLast(3);
            var node = new Node <int> {
                Value = 5
            };

            customLinked      = new CustomLinkedList <int>();
            customLinked.Head = new Node <int> {
                Value = 1
            };
            customLinked.Head.Next = new Node <int> {
                Value = 2
            };
            customLinked.Head.Next.Next = new Node <int> {
                Value = 2
            };
            customLinked.Head.Next.Next.Next      = node;
            customLinked.Head.Next.Next.Next.Next = new Node <int> {
                Value = 3
            };
            customLinked2      = new CustomLinkedList <int>();
            customLinked2.Head = new Node <int> {
                Value = 7
            };
            customLinked2.Head.Next = new Node <int> {
                Value = 8
            };
            customLinked2.Head.Next.Next = new Node <int> {
                Value = 9
            };
            customLinked2.Head.Next.Next.Next      = node;
            customLinked2.Head.Next.Next.Next.Next = new Node <int> {
                Value = 3
            };

            PrintHelpers.PrintArray("Intersection Point of 2 Linked list in Custom Linked List", new[] { customLinked, customLinked2 },
                                    new CustomLinkedList <int>
            {
                Head = new Node <int> {
                    Value = IntersectionPointOfTwoLists.GetIntersectionNode(customLinked, customLinked2).Value
                }
            });


            //Merge two Sorted Arrays
            linked = new LinkedList <int>();
            linked.AddLast(1);
            linked.AddLast(4);
            linked.AddLast(8);
            linked.AddLast(11);
            linked2 = new LinkedList <int>();
            linked2.AddLast(2);
            linked2.AddLast(4);
            linked2.AddLast(5);
            linked2.AddLast(9);
            PrintHelpers.PrintArray("Merge Two Sorted Arrays", new [] { linked.ToArray(), linked2.ToArray() }, MergeTwoSortedLinkedList.GetMergedList(linked, linked2).ToArray());

            customLinked      = new CustomLinkedList <int>();
            customLinked.Head = new Node <int> {
                Value = 1
            };
            customLinked.Head.Next = new Node <int> {
                Value = 4
            };
            customLinked.Head.Next.Next = new Node <int> {
                Value = 8
            };
            customLinked.Head.Next.Next.Next = new Node <int> {
                Value = 11
            };
            customLinked2      = new CustomLinkedList <int>();
            customLinked2.Head = new Node <int> {
                Value = 2
            };
            customLinked2.Head.Next = new Node <int> {
                Value = 4
            };
            customLinked2.Head.Next.Next = new Node <int> {
                Value = 5
            };
            customLinked2.Head.Next.Next.Next = new Node <int> {
                Value = 9
            };
            PrintHelpers.PrintArray("Merge Two Sorted Arrays in Custom Linked List", new [] { customLinked, customLinked2 }, MergeTwoSortedLinkedList.GetMergedList(customLinked, customLinked2));

            // Rotata a linked list
            customLinked      = new CustomLinkedList <int>();
            customLinked.Head = new Node <int> {
                Value = 1
            };
            customLinked.Head.Next = new Node <int> {
                Value = 2
            };
            customLinked.Head.Next.Next = new Node <int> {
                Value = 3
            };
            customLinked.Head.Next.Next.Next = new Node <int> {
                Value = 4
            };
            customLinked2      = new CustomLinkedList <int>();
            customLinked2.Head = new Node <int> {
                Value = 1
            };
            customLinked2.Head.Next = new Node <int> {
                Value = 2
            };
            customLinked2.Head.Next.Next = new Node <int> {
                Value = 3
            };
            customLinked2.Head.Next.Next.Next = new Node <int> {
                Value = 4
            };
            PrintHelpers.PrintArray("Rotate in Custom Linked List", customLinked2, RotateLinkedList.Rotate(customLinked, 2));
        }
예제 #2
0
        public static void MathAndStatsProblems()
        {
            PrintHelpers.PrintArray("All Sum Combination", new[] { 5 }, AllSumsCombination.GetAllCombinations(5));
            PrintHelpers.PrintArray("All Sum Combination 2", new[] { 4 }, AllSumsCombination.GetAllCombinations(4));

            Console.WriteLine("Kth Permutation Recusive");
            List <char> input = new List <char> {
                '1', '2', '3', '4'
            };

            Console.WriteLine("Input: { '1', '2', '3', '4' } ");
            var sb = new System.Text.StringBuilder();

            KthPermutation.Find_kth_permutation_rec(input, 7, sb);
            Console.WriteLine("Output: " + sb);

            Console.WriteLine("");

            Console.WriteLine("Kth Permutation");
            input = new List <char> {
                '1', '2', '3', '4'
            };
            Console.WriteLine("Input: { '1', '2', '3', '4' } ");
            sb = new System.Text.StringBuilder();
            KthPermutation.Find_kth_permutation_rec(input, 7, sb);
            Console.WriteLine("Output: " + sb);

            Console.WriteLine("Kth Permutation 2");
            input = new List <char> {
                '1', '2', '3'
            };
            Console.WriteLine("Input: { '1', '2', '3'} ");
            sb = new System.Text.StringBuilder();
            KthPermutation.Find_kth_permutation_rec(input, 4, sb);
            Console.WriteLine("Output: " + sb);

            Console.WriteLine("");
            Console.WriteLine("Integer Division");
            Console.WriteLine("Input: Dividen: 40; Divisor: 4");
            Console.WriteLine("Output: " + IntegerDivision.IntegerDivisionRec(40, 4));
            Console.WriteLine("");

            Console.WriteLine("");
            Console.WriteLine("Integer Division 2");
            Console.WriteLine("Input: Dividen: 7; Divisor: 3 ");
            Console.WriteLine("Output: " + IntegerDivision.IntegerDivisionRec(7, 3));
            Console.WriteLine("");

            var listInput = new List <int> {
                4, 6, 8, 1, 3, 5, 10, 9, 7
            };

            PrintHelpers.PrintArray("Pythagoras Triplet", listInput, PythagorasTriplet.GetPythagorasTriplets(listInput));

            Console.WriteLine("");
            Console.WriteLine("Find missing number");
            Console.WriteLine("Input: { 4, 6, 8, 1, 3, 5, 10, 9, 7} ");
            Console.WriteLine("Output: ");
            Console.Write(FindMissingNumber.Find_missing(listInput));


            var subsetInput = new List <int> {
                2, 3, 4
            };

            PrintHelpers.PrintArray("All Subsets", subsetInput, AllSubsets.Get_all_subsets(subsetInput));

            PrintHelpers.PrintArray("Permute String", new List <string> {
                "a", "b", "c"
            }, PermuteString.Permute_string("abc"));

            Console.WriteLine("");
            Console.WriteLine("Is Valid number");
            Console.WriteLine("Input: ++4456.054");
            Console.Write("Output: ");
            Console.Write(NumberValid.IsNumberValid("++4456.054"));
            Console.WriteLine("");

            Console.WriteLine("Is Valid number 2");
            Console.WriteLine("Input: -4456.054");
            Console.Write("Output: ");
            Console.Write(NumberValid.IsNumberValid("-4456.054"));
            Console.WriteLine("");

            Console.WriteLine("");
            Console.WriteLine("Power of Number");
            Console.WriteLine("Input: 3,2");
            Console.Write("Output: ");
            Console.Write(PowerOfNumber.Power(3, 2));
            Console.WriteLine("");

            Console.WriteLine("");
            Console.WriteLine("Square root of Number");
            Console.WriteLine("Input: 2.2500");
            Console.Write("Output: ");
            Console.Write(SquareRootOfNumber.GetSquareRoot(2.2500));
            Console.WriteLine("");
        }
        public static void ArrayProblems()
        {
            //Arrays

            //Binary Search
            int[] input = { 1, 3, 7, 9, 10, 15, 20 };
            PrintHelpers.PrintArray("Binary Search", input, new[] { BinarySearch.GetElementIndex(input, 9) }, "K = 9");
            PrintHelpers.PrintArray("Binary Search Recursively", input, new[] { BinarySearch.GetElementIndexRecursively(input, 9) }, "K = 9");

            // Maximum Sliding Window
            input = new int[] { 8, 5, 10, 7, 9, 4, 15, 12, 90, 13 };
            var ouput = MaximumSlidingWindow.GetMaximumElementPerWindow(input, 4);

            PrintHelpers.PrintArray("Maximum Sliding Window", input, ouput, "Window Size = 4");

            //Sorted Rotate Array Search
            input = new int[] { 4, 5, 6, 7, 8, 1, 2, 3 };
            PrintHelpers.PrintArray("Sorted Rotate Array", input, new[] { SortedRotatedArray.GetElementIndex(input, 8) }, "K = 8");

            //Common elements in sorted Array
            int[] array1 = { 1, 5, 10, 20, 40, 80 };
            int[] array2 = { 6, 7, 20, 80, 100 };
            int[] array3 = { 3, 4, 15, 20, 30, 70, 80, 120 };
            PrintHelpers.PrintArray("Common elements in sorted Array", new[] { array1, array2, array3 }, CommonElementsInThreeSortedArray.GetCommonElements(array1, array2, array3));


            //Rotated Array
            input = new int[] { 1, 3, 7, 9, 10, 15, 20 };
            int[] input2 = { 1, 3, 7, 9, 10, 15, 20 };
            PrintHelpers.PrintArray("Rotate Array", input, RotateArray.GetRotateArray(input2, 3), "Pivot = 3");

            //First and Last Index
            input = new int[] { 1, 3, 5, 5, 5, 5, 7, 123, 125 };
            PrintHelpers.PrintArray("First and Last Index for input", input, FirstAndLast.FindFirstAndLast(input, 7), "Input = 7");

            //Move Zeros to Left
            input  = new int[] { 1, 2, 3, 0, 0, 0, 4, 5 };
            input2 = new int[] { 1, 2, 3, 0, 0, 0, 4, 5 };
            PrintHelpers.PrintArray("Move Zeros to Left", input2, MoveZeroesToLeft.ShiftZeros(input));

            //Buy Sell max Profit
            input = new int[] { 10, 22, 5, 75, 65, 80 };
            PrintHelpers.PrintArray("Buy Sell max Profit", input, BuySellMaxProfit.GetBuySellIndex(input));

            //Multiple Buy Sell max Profit
            input = new int[] { 10, 22, 8, 5, 75, 65, 80 };
            PrintHelpers.PrintArray("Multiple Buy Sell max Profit", input, BuySellMaxProfit.GetAllBuySellIndex(input));

            //Sum of Two Values
            input = new int[] { 5, 7, 1, 2, 8, 4, 3 };
            PrintHelpers.PrintArray("Sum of Two Values", input, SumOfTwoValues.GetPairs(input, 10), "Sum = 10");

            //Merging Intervals
            var inputList = new List <List <int> >();

            inputList.Add(new List <int> {
                1, 3
            });
            inputList.Add(new List <int> {
                2, 4
            });
            inputList.Add(new List <int> {
                5, 7
            });
            inputList.Add(new List <int> {
                6, 8
            });
            var inputList2 = new List <List <int> >();

            inputList.Add(new List <int> {
                1, 3
            });
            inputList.Add(new List <int> {
                2, 4
            });
            inputList.Add(new List <int> {
                5, 7
            });
            inputList.Add(new List <int> {
                6, 8
            });
            PrintHelpers.PrintArrays("Merging Intervals", inputList2, MergeIntervals.Merge(inputList));

            //Quick Sort
            input  = new int[] { 5, 7, 1, 2, 8, 4, 3 };
            input2 = new int[] { 5, 7, 1, 2, 8, 4, 3 };
            PrintHelpers.PrintArray("Quick Sort", input2, QuickSort.Sort(input));

            //Quick Sort
            input  = new int[] { 5, 7, 1, 2, 8, 4, 3 };
            input2 = new int[] { 5, 7, 1, 2, 8, 4, 3 };
            PrintHelpers.PrintArray("Merge Sort", input2, MergeSort.Merge(input));

            //Sum of Three Values
            input  = new int[] { 5, 7, 1, 2, 8, 4, 3 };
            input2 = new int[] { 5, 7, 1, 2, 8, 4, 3 };
            PrintHelpers.PrintArray("Sum of Three Values", input2, SumOfThreeValues.SumOfThreeValue(input, 10));
        }