コード例 #1
0
        public void SumListReverseRecursiveTest(Node <int> input1, Node <int> input2, Node <int> expected)
        {
            Node <int> totalResult = null;

            totalResult = SumList.SumReverse(input1, input2);
            Assert.Equal(expected, totalResult);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var condition = true;

            //just keeping the current solving problem in if condition; nothing else
            if (condition)
            {
                KStacks.MainMethod();
            }
            else
            {
                #region LinkedLists
                LinkIntersection.MainMethod();
                SumList.MainMethod();
                RemoveDups <int> .MainMethod();

                ReturnKthToLast.MainMethod(1);
                DeleteMiddleNode.MainMethod();
                LoopDetection.MainMethod();
                #endregion

                #region Array and Strings
                StringRotation.IsStringRotation("waterbottle", "erbottlewat");
                ZeroMatrixImplementation();
                RotateMatrixImplementation(4);
                StringCompression.CompressedString("aabcccccaaa");
                OneAway.IsStringOneAway("pale", "paled");
                PalindromePermutation.IsPalindromePermutation("Mr. owl ate my Metal worm");
                URLify.URLifyString("Spaces in this string will be replaced by percent20");
                IsStringPermutation.VerifyStringPermutation("abdcdefgh", "aefgb2cdh");
                UniqueString.VerifyUniqueStringAsciiSet("!@#$%$^&*()EFgh");
                HashTableImplentation();
                SwapWithoutTemp.SwapWithoutTempVar(12, 24);
                #endregion
            }
        }
コード例 #3
0
 public void Setup()
 {
     sumList = new SumList();
 }
コード例 #4
0
    /**
     * Checks if a combo exists in this numberList.
     * @param startIndex the index to check from
     * @param target the target to reach
     * @return true if combo exists, false otherwise.
     */
    public bool CheckForComboAt(int startIndex, int target)
    {
        RefreshArray();
        // SumList objects store a sum and the index they're found on.
        SumList sumListLeft = new SumList(), sumListRight = new SumList();

        sumListLeft.Add(0, -1);
        sumListRight.Add(0, -1);

        /**
         * Disregard all the numbers that cannot be part of the combo and return
         * the shorter array with numbers that can.
         */
        ShortList sl = FindShortList(startIndex, target);

        NumberNode[] shortList = sl.array;

        // Set the index to start counting from for counting towards
        // left and counting towards right.
        int leftStartIndex  = sl.GetStartIndex();
        int rightStartIndex = sl.GetStartIndex() + 1;

        // Create a temporary sum.
        int sum = 0;

        // Go over all numbers in the short list left of (and including) the start index
        for (int i = leftStartIndex; i > -1; i--)
        {
            // Increase the sum by the current value
            sum += shortList[i].value;
            // Store the sum and at what index in the shortList the sum occured
            sumListLeft.Add(sum, i);
        }
        // Reset the temporary sum.
        sum = 0;
        // Go over all numbers in the short list right of (and excluding) the start index
        for (int i = rightStartIndex; i < shortList.Length; i++)
        {
            // Increase the sum by the current value
            sum += shortList[i].value;
            // Store the sum and at what index in the shortList the sum occured
            sumListRight.Add(sum, i);
        }

        // For both sumlists...
        for (int sumListLeftIndex = 0; sumListLeftIndex < sumListLeft.sumList.Count; sumListLeftIndex++)
        {
            for (int sumListRightIndex = 0; sumListRightIndex < sumListRight.sumList.Count; sumListRightIndex++)
            {
                // Get the value of the currently selected sums added together
                int current_sum = sumListLeft.sumList[sumListLeftIndex] + sumListRight.sumList[sumListRightIndex];

                if (current_sum == target)
                {
                    // Found a combo!
                    // Kill all nodes that make up this combination.
                    for (int k = 0; k < sumListLeftIndex + 1; k++)
                    {
                        int index = sumListLeft.indexList[k];
                        if (index > -1)
                        {
                            shortList[index].Kill();
                        }
                    }
                    for (int k = 0; k < sumListRightIndex + 1; k++)
                    {
                        int index = sumListRight.indexList[k];
                        if (index > -1)
                        {
                            shortList[index].Kill();
                        }
                    }
                    return(true);
                }
                // If the combined sum is above it's target, no combo can be found, so
                // stop looking any further.
                else if (current_sum > target)
                {
                    return(false);
                }
            }
        }
        return(false);
    }
コード例 #5
0
        public void SumListRecursiveTest(Node <int> input1, Node <int> input2, Node <int> expected)
        {
            Node <int> sumList = SumList.SumRecursive(input1, input2, 0);

            Assert.Equal(expected, sumList);
        }
コード例 #6
0
        public void SumListTest(Node <int> input1, Node <int> input2, Node <int> expected)
        {
            Node <int> sumList = SumList.Sum(input1, input2);

            Assert.Equal(expected, sumList);
        }
コード例 #7
0
 public UnitTest1()
 {
     sumList = new SumList();
 }