Esempio n. 1
0
        public static void AbsDistinct()
        {
            int[] arr = new int[] { -3, -3, 3, 3 };
            Assert.That(ArrayQ.AbsDistinct(arr) == 1);

            Assert.That(ArrayQ.AbsDistinct(new int[] { -5, -3, -1, 0, 3, 6}) == 5);
        }
Esempio n. 2
0
        public void PlusOneTest2()
        {
            var p        = new ArrayQ();
            var testFunc = new PlusOneDel(p.PlusOne2);

            PlusOneTestDriver(testFunc);
        }
Esempio n. 3
0
        public static void FindKthLargest()
        {
            ArrayQ arrayQ = new ArrayQ();

            int[] arr = { 3, 2, 1, 5, 6, 4 };
            int   res = arrayQ.FindKthLargest(arr, 2);
        }
Esempio n. 4
0
 public void MaximumGap()
 {
     Assert.That(ArrayQ.MaximumGap(new List <int>()
     {
         3, 5, 4, 2
     }) == 2);
 }
        public void RemoveDuplicates4Test()
        {
            var q    = new ArrayQ();
            var test = new RemoveDuplicateFuncTests(q.RemoveDuplicates4);

            RunRemoveDuplicateTests(test);
        }
Esempio n. 6
0
        public void MergeTest()
        {
            var testP = new ArrayQ();
            var nums1 = new int[] { 1, 2, 3, 0, 0, 0 };
            var m     = 3;
            var nums2 = new int[] { 2, 5, 6 };
            var n     = 3;

            testP.Merge(nums1, m, nums2, n);
            Assert.AreEqual("1 2 2 3 5 6", string.Join(" ", nums1));

            nums1 = new int[] { 1, 2, 3, 0, 0, 0 };
            m     = 3;
            nums2 = new int[] { 2, 5 };
            n     = 2;
            testP.Merge(nums1, m, nums2, n);
            Assert.AreEqual("1 2 2 3 5 0", string.Join(" ", nums1));

            nums1 = new int[] { 6, 7, 8, 0, 0, 0 };
            m     = 3;
            nums2 = new int[] { 2, 3, 5 };
            n     = 3;
            testP.Merge(nums1, m, nums2, n);
            Assert.AreEqual("2 3 5 6 7 8", string.Join(" ", nums1));

            nums1 = new int[] { 6, 7, 8, 0, 0, 0 };
            m     = 3;
            nums2 = new int[] { 9, 10, 11 };
            n     = 3;
            testP.Merge(nums1, m, nums2, n);
            Assert.AreEqual("6 7 8 9 10 11", string.Join(" ", nums1));

            nums1 = new int[] { 6, 7, 12, 0, 0, 0 };
            m     = 3;
            nums2 = new int[] { 9, 10, 11 };
            n     = 3;
            testP.Merge(nums1, m, nums2, n);
            Assert.AreEqual("6 7 9 10 11 12", string.Join(" ", nums1));

            nums1 = new int[] { 6, 7, 12, 0, 0, 0 };
            m     = 3;
            nums2 = new int[] { 8, 8, 8 };
            n     = 3;
            testP.Merge(nums1, m, nums2, n);
            Assert.AreEqual("6 7 8 8 8 12", string.Join(" ", nums1));

            nums1 = new int[] { 8, 8, 8, 0, 0, 0 };
            m     = 3;
            nums2 = new int[] { 9, 10, 11 };
            n     = 3;
            testP.Merge(nums1, m, nums2, n);
            Assert.AreEqual("8 8 8 9 10 11", string.Join(" ", nums1));

            nums1 = new int[] { -1, 0, 0, 3, 3, 3, 0, 0, 0 };
            m     = 6;
            nums2 = new int[] { 1, 2, 2 };
            n     = 3;
            testP.Merge(nums1, m, nums2, n);
            Assert.AreEqual("-1 0 0 1 2 2 3 3 3", string.Join(" ", nums1));
        }
Esempio n. 7
0
 public static void Fibonacchi()
 {
     Assert.That(ArrayQ.Fibonacci(0) == 0);
     Assert.That(ArrayQ.Fibonacci(1) == 1);
     Assert.That(ArrayQ.Fibonacci(2) == 1);
     Assert.That(ArrayQ.Fibonacci(3) == 2);
     Assert.That(ArrayQ.Fibonacci(8) == 21);
 }
Esempio n. 8
0
        public void SearchInsertTest()
        {
            var testFunc = new ArrayQ();

            Assert.AreEqual(2, testFunc.SearchInsert(new int[] { 1, 3, 5, 6 }, 5));
            Assert.AreEqual(1, testFunc.SearchInsert(new int[] { 1, 3, 5, 6 }, 2));
            Assert.AreEqual(4, testFunc.SearchInsert(new int[] { 1, 3, 5, 6 }, 7));
            Assert.AreEqual(0, testFunc.SearchInsert(new int[] { 1, 3, 5, 6 }, 0));
        }
Esempio n. 9
0
        public void LengthOfLastWordTest()
        {
            var testCase = new ArrayQ();

            Assert.AreEqual(5, testCase.LengthOfLastWord("Hello World"));
            Assert.AreEqual(0, testCase.LengthOfLastWord(""));
            Assert.AreEqual(0, testCase.LengthOfLastWord(" "));
            Assert.AreEqual(6, testCase.LengthOfLastWord("How are you doing?"));
            Assert.AreEqual(6, testCase.LengthOfLastWord("How are you doing? "));
        }
Esempio n. 10
0
        public static void RainWaterTrap()
        {
            List <int> traps = new List <int>()
            {
                0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1
            };

            Assert.That(ArrayQ.RainWaterTrap(traps) == 6);
            Assert.That(ArrayQ.RainWaterTrap2(traps) == 6);
        }
Esempio n. 11
0
        public void LongestCommonPrefixTest()
        {
            var q        = new ArrayQ();
            var testCase = new string[] { "TotalPackage" };

            Assert.AreEqual("TotalPackage", q.LongestCommonPrefix(testCase));
            testCase = new string[] { "flower", "flow", "flight" };
            Assert.AreEqual("fl", q.LongestCommonPrefix(testCase));
            testCase = new string[] { "dog", "racecar", "car" };
            Assert.AreEqual("", q.LongestCommonPrefix(testCase));
        }
Esempio n. 12
0
        public void RemoveElementTest()
        {
            var testFunc = new ArrayQ();
            var testcase = new int[] { 3, 2, 2, 3 };
            var output   = testFunc.RemoveElement(testcase, 3);

            Assert.AreEqual(2, output);
            var result = GenerateResultIntOutput(testcase, output);

            ValidateArrayElements(result, new int[] { 2, 2 });

            testcase = new int[] { 0, 1, 2, 2, 3, 0, 4, 2 };
            output   = testFunc.RemoveElement(testcase, 2);
            Assert.AreEqual(5, output);
            result = GenerateResultIntOutput(testcase, output);
            ValidateArrayElements(result, new int[] { 0, 1, 3, 0, 4 });

            testcase = new int[] { 3, 2, 2, 3 };
            output   = testFunc.RemoveElement(testcase, 2);
            Assert.AreEqual(2, output);
            result = GenerateResultIntOutput(testcase, output);
            ValidateArrayElements(result, new int[] { 3, 3 });

            testcase = new int[] { };
            output   = testFunc.RemoveElement(testcase, 2);
            Assert.AreEqual(0, output);
            result = GenerateResultIntOutput(testcase, output);
            ValidateArrayElements(result, new int[] { });

            testcase = new int[] { 1 };
            output   = testFunc.RemoveElement(testcase, 2);
            Assert.AreEqual(1, output);
            result = GenerateResultIntOutput(testcase, output);
            ValidateArrayElements(result, new int[] { 1 });

            testcase = new int[] { 1 };
            output   = testFunc.RemoveElement(testcase, 1);
            Assert.AreEqual(0, output);
            result = GenerateResultIntOutput(testcase, output);
            ValidateArrayElements(result, new int[] { });

            testcase = new int[] { 3, 3, 1, 2, 4, 3, 3 };
            output   = testFunc.RemoveElement(testcase, 3);
            Assert.AreEqual(3, output);
            result = GenerateResultIntOutput(testcase, output);
            ValidateArrayElements(result, new int[] { 1, 2, 4 });

            testcase = new int[] { 3, 3, 1, 2, 3, 4, 3, 3 };
            output   = testFunc.RemoveElement(testcase, 3);
            Assert.AreEqual(3, output);
            result = GenerateResultIntOutput(testcase, output);
            ValidateArrayElements(result, new int[] { 1, 2, 4 });
        }
Esempio n. 13
0
        public void IsValidTest()
        {
            var q = new ArrayQ();

            Assert.AreEqual(true, q.IsValid(""));
            Assert.AreEqual(true, q.IsValid("()"));
            Assert.AreEqual(true, q.IsValid("()[]{}"));
            Assert.AreEqual(false, q.IsValid("(]"));
            Assert.AreEqual(false, q.IsValid("([)]"));
            Assert.AreEqual(true, q.IsValid("{[]}"));
            Assert.AreEqual(false, q.IsValid("(("));
        }
Esempio n. 14
0
        public void RomanToIntTest()
        {
            var q = new ArrayQ();

            Assert.AreEqual(q.RomanToInt("I"), 1);
            Assert.AreEqual(q.RomanToInt("i"), 1);
            Assert.AreEqual(q.RomanToInt("III"), 3);
            Assert.AreEqual(q.RomanToInt("IV"), 4);
            Assert.AreEqual(q.RomanToInt("IX"), 9);
            Assert.AreEqual(q.RomanToInt("LVIII"), 58);
            Assert.AreEqual(q.RomanToInt("MCMXCIV"), 1994);
        }
Esempio n. 15
0
        public void ReverseTest()
        {
            var q = new ArrayQ();

            Assert.AreEqual(q.Reverse(1), 1);
            Assert.AreEqual(q.Reverse(-1), -1);
            Assert.AreEqual(q.Reverse(12), 21);
            Assert.AreEqual(q.Reverse(123), 321);
            Assert.AreEqual(q.Reverse(-123), -321);
            Assert.AreEqual(q.Reverse(120), 21);
            Assert.AreEqual(q.Reverse(1534236469), 0);
        }
Esempio n. 16
0
        public void AddBinaryTest()
        {
            var p = new ArrayQ();

            Assert.AreEqual("100", p.AddBinary("1", "11"));
            Assert.AreEqual("10101", p.AddBinary("1010", "1011"));
            Assert.AreEqual("110", p.AddBinary("11", "11"));
            Assert.AreEqual("11", p.AddBinary("11", ""));
            Assert.AreEqual("11", p.AddBinary("", "11"));
            Assert.AreEqual("0", p.AddBinary("0", "0"));
            Assert.AreEqual("10", p.AddBinary("1", "1"));
        }
Esempio n. 17
0
        public static void RotateMatrix()
        {
            int[,] matrix0 = { { 1, 2 }, { 3, 4 } };
            int[,] matrix1 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
            int[,] matrix2 = { { 31, 32, 228, 333 }, { 79, 197, 241, 246 }, { 77, 84, 126, 337 }, { 110, 291, 356, 371 } };

            ArrayQ.RotateMatrix(matrix2);
            ArrayQ.RotateMatrix(matrix1);

            int[,] matrix3 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
            ArrayQ.RotateMatrixDiagonal(matrix3);
        }
Esempio n. 18
0
        public void StrStrTest()
        {
            var testFunc = new ArrayQ();

            Assert.AreEqual(-1, testFunc.StrStr(string.Empty, "ll"));
            Assert.AreEqual(0, testFunc.StrStr("hello", string.Empty));
            Assert.AreEqual(0, testFunc.StrStr(string.Empty, string.Empty));
            Assert.AreEqual(2, testFunc.StrStr("hello", "ll"));
            Assert.AreEqual(-1, testFunc.StrStr("aaaaa", "bba"));
            Assert.AreEqual(0, testFunc.StrStr("aaaaa", "aaa"));
            Assert.AreEqual(1, testFunc.StrStr("baaaaa", "aaa"));
        }
Esempio n. 19
0
        public void IsPalindromeTest()
        {
            var q = new ArrayQ();

            Assert.AreEqual(q.IsPalindrome(1), true);
            Assert.AreEqual(q.IsPalindrome(-1), false);
            Assert.AreEqual(q.IsPalindrome(12), false);
            Assert.AreEqual(q.IsPalindrome(121), true);
            Assert.AreEqual(q.IsPalindrome(-121), false);
            Assert.AreEqual(q.IsPalindrome(10), false);
            Assert.AreEqual(q.IsPalindrome(1234321), true);
            Assert.AreEqual(q.IsPalindrome(12344321), true);
        }
Esempio n. 20
0
        public void CountAndSayTest()
        {
            var testFunc = new ArrayQ();

            Assert.AreEqual("0", testFunc.CountAndSay(0));
            Assert.AreEqual("1", testFunc.CountAndSay(1));
            Assert.AreEqual("11", testFunc.CountAndSay(2));
            Assert.AreEqual("21", testFunc.CountAndSay(3));
            Assert.AreEqual("1211", testFunc.CountAndSay(4));
            Assert.AreEqual("111221", testFunc.CountAndSay(5));
            Assert.AreEqual("312211", testFunc.CountAndSay(6));
            Assert.AreEqual("13112221", testFunc.CountAndSay(7));
            Assert.AreEqual("1113213211", testFunc.CountAndSay(8));
        }
Esempio n. 21
0
        public void ClimbStairsTest()
        {
            var testp = new ArrayQ();

            Assert.AreEqual(1, testp.ClimbStairs(1));
            Assert.AreEqual(2, testp.ClimbStairs(2));
            Assert.AreEqual(3, testp.ClimbStairs(3));
            Assert.AreEqual(5, testp.ClimbStairs(4));
            Assert.AreEqual(8, testp.ClimbStairs(5));
            Assert.AreEqual(13, testp.ClimbStairs(6));
            Assert.AreEqual(21, testp.ClimbStairs(7));
            Assert.AreEqual(34, testp.ClimbStairs(8));
            Assert.AreEqual(55, testp.ClimbStairs(9));
            Assert.AreEqual(89, testp.ClimbStairs(10));
        }
Esempio n. 22
0
        public void RunRemoveDuplicateTests(RemoveDuplicateFuncTests testDel)
        {
            var q        = new ArrayQ();
            var testcase = new int[] { 1, 1, 2 };
            var output   = testDel.Invoke(testcase);

            Assert.AreEqual(2, output);
            ValidateArrayElements(new int[] { 1, 2 }, GenerateResultIntOutput(testcase, output));

            testcase = new int[] { 0, 0, 1, 1, 1, 2, 2, 3, 3, 4 };
            output   = testDel.Invoke(testcase);
            Assert.AreEqual(5, output);
            ValidateArrayElements(new int[] { 0, 1, 2, 3, 4 }, GenerateResultIntOutput(testcase, output));


            testcase = new[] { 1, 1 };
            output   = testDel.Invoke(testcase);
            Assert.AreEqual(1, output);
            ValidateArrayElements(new int[] { 1 }, GenerateResultIntOutput(testcase, output));


            testcase = new[] { 1, 2, 2 };
            output   = testDel.Invoke(testcase);
            Assert.AreEqual(2, output);
            ValidateArrayElements(new int[] { 1, 2 }, GenerateResultIntOutput(testcase, output));


            testcase = new[] { 1, 1, 2, 2 };
            output   = testDel.Invoke(testcase);
            Assert.AreEqual(2, output);
            ValidateArrayElements(new int[] { 1, 2 }, GenerateResultIntOutput(testcase, output));


            testcase = new[] { 1, 2, 2, 3 };
            output   = testDel.Invoke(testcase);
            Assert.AreEqual(3, output);
            ValidateArrayElements(new int[] { 1, 2, 3 }, GenerateResultIntOutput(testcase, output));

            testcase = new[] { 1, 1, 1 };
            output   = testDel.Invoke(testcase);
            Assert.AreEqual(1, output);
            ValidateArrayElements(new int[] { 1 }, GenerateResultIntOutput(testcase, output));

            testcase = new[] { -3, -3, -2, -1, -1, 0, 0, 0, 0, 0 };
            output   = testDel.Invoke(testcase);
            Assert.AreEqual(4, output);
            ValidateArrayElements(new int[] { -3, -2, -1, 0 }, GenerateResultIntOutput(testcase, output));
        }
Esempio n. 23
0
        public void TwoSumTest()
        {
            var test = new ArrayQ();

            Assert.AreEqual(null, test.TwoSum(new int[] { }, 10));
            Assert.AreEqual(null, test.TwoSum(new int[] { 1 }, 10));
            Assert.AreEqual(null, test.TwoSum(new int[] { 2, 3 }, 10));
            Assert.AreEqual(string.Join(", ", new int[] { 0, 1 }).Trim(),
                            string.Join(", ", test.TwoSum(new int[] { 5, 5 }, 10)).Trim());
            Assert.AreEqual(string.Join(", ", new int[] { 0, 1 }).Trim(),
                            string.Join(", ", test.TwoSum(new int[] { 2, 7, 11, 15 }, 9)).Trim());

            Assert.AreEqual(string.Join(", ", new int[] { 0, 3 }).Trim(),
                            string.Join(", ", test.TwoSum(new int[] { 2, 11, 15, 7 }, 9)).Trim());

            Assert.AreEqual(string.Join(", ", new int[] { 0, 3 }).Trim(),
                            string.Join(", ", test.TwoSum(new int[] { 7, 11, 15, 2, 7 }, 9)).Trim());

            Assert.AreEqual(string.Join(", ", new int[] { 1, 2 }).Trim(),
                            string.Join(", ", test.TwoSum(new int[] { 3, 2, 4 }, 6)).Trim());
        }
Esempio n. 24
0
        public void MergeTwoListsTest()
        {
            var q           = new ArrayQ();
            var linkedList1 = new ListNode(1)
            {
                next = new ListNode(2)
            };

            linkedList1.next.next = new ListNode(4);

            var linkedList2 = new ListNode(1)
            {
                next = new ListNode(3)
            };

            linkedList2.next.next = new ListNode(4);

            var mergedList = q.MergeTwoLists(linkedList1, linkedList2);

            Assert.AreEqual("1 1 2 3 4 4", GetLinkedListItems(mergedList));

            linkedList1 = null;
            linkedList2 = new ListNode(0)
            {
                next = null
            };
            mergedList = q.MergeTwoLists(linkedList1, linkedList2);
            Assert.AreEqual("0", GetLinkedListItems(mergedList));

            linkedList2 = null;
            linkedList1 = new ListNode(0)
            {
                next = null
            };
            mergedList = q.MergeTwoLists(linkedList1, linkedList2);
            Assert.AreEqual("0", GetLinkedListItems(mergedList));
        }
Esempio n. 25
0
        public static void SetToZeroRowsColumns()
        {
            int[,] matrix = { { 0, 1, 0 }, { 2, 3, 4 }, { 5, 0, 6 } };
            ArrayQ.SetToZeroRowsColumns(matrix);

            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Assert.That(matrix[i, j] == 0);
                }
            }

            int[,] matrix1 = { { 0, 1, 0 }, { 2, 3, 4 }, { 5, 0, 6 } };
            ArrayQ.SetToZeroRowsColumnsTwoBools(matrix1);

            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Assert.That(matrix1[i, j] == 0);
                }
            }
        }
Esempio n. 26
0
 public static void MinimumBrabes()
 {
     int[] arr = new int[] { 1, 2, 5, 3, 4 };
     Assert.That(ArrayQ.minimumBribes(arr) == 2);
 }
Esempio n. 27
0
        public static void SearchInRotatedSortedArray()
        {
            int index = ArrayQ.SearchInRotatedSortedArray(new int[] { 7, 1, 2, 3, 4, 5, 6 }, 2);

            Assert.That(index == 2);
        }
Esempio n. 28
0
 public void LongestSubsequence()
 {
     int[] array1 = { 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 };
     Assert.That(ArrayQ.LongestSubsequence(array1.ToList()) == 6);
 }
Esempio n. 29
0
 public void MaxSlidingWindow()
 {
     int[] array1 = { 1, 3, 1, 2, 0, 5 };
     var   res    = ArrayQ.MaxSlidingWindow(array1, 3);
 }
Esempio n. 30
0
 public void FractionToDecimal()
 {
     Assert.That(ArrayQ.FractionToDecimal(-2147483648, -1) == "2147483648");
     Assert.That(ArrayQ.FractionToDecimal(-1, 3) == "-0.(3)");
 }