Example #1
0
        public void LeetInput_1()
        {
            // Arrange
            int[] nums = new int[] { 3, 0, -2, -1, 1, 2 };
            _3Sum algo = new _3Sum(true);

            // Act
            IList <IList <int> > triplets = algo.ThreeSum(nums);

            //Assert
            Assert.AreEqual(3, triplets.Count);

            // -2,-1,3
            Assert.AreEqual(-2, triplets[0][0]);
            Assert.AreEqual(-1, triplets[0][1]);
            Assert.AreEqual(3, triplets[0][2]);

            // -2, 0, 2
            Assert.AreEqual(-2, triplets[1][0]);
            Assert.AreEqual(0, triplets[1][1]);
            Assert.AreEqual(2, triplets[1][2]);

            // -1, 0, 1
            Assert.AreEqual(-1, triplets[2][0]);
            Assert.AreEqual(0, triplets[2][1]);
            Assert.AreEqual(1, triplets[2][2]);
        }
Example #2
0
        public void ThreeSumTests()
        {
            int[] nums = new int[] { -4, -1, -1, 0, 1, 2 };

            //            solution set is:
            //[
            //  [-1, 0, 1],
            //  [-1, -1, 2]
            //]

            _3Sum obj = new _3Sum();

            var x = obj.ThreeSum(nums);


            //            solution set is:
            //[
            //  [-1, 0, 1],
            //  [-1, -1, 2]
            //]
            nums = new int[] { -4, -1, -1, -1, 0, 1, 2 };
            x    = obj.ThreeSum(nums);



            nums = new int[] { -4, 0, 1, 2, 2, 3 };// -4,2,2   -4,1,3
            x    = obj.ThreeSum(nums);
        }
Example #3
0
        public void Execute()
        {
            Console.WriteLine($"\n{nameof(_3Sum_Test)}");
            int[] arr = new int[] { -1, 0, 1, 2, -1, -4 };
            IList <IList <int> > result = new _3Sum().ThreeSum(arr);

            Console.WriteLine($"Input array: {String.Join(", ", arr)}");
            Console.WriteLine($"Result: {String.Join("\t", result.Select(r => String.Join(", ", r)))}");
        }
Example #4
0
        public void ThreeSum_Test()
        {
            // Arrange
            int[] nums = new int[] { -1, 0, 1, 2, -1, -4 };
            _3Sum algo = new _3Sum(true);

            // Act
            IList <IList <int> > triplets = algo.ThreeSum(nums);    // as List<List<int>>;

            // Assert
            Assert.AreEqual(2, triplets.Count);
        }
Example #5
0
        public void CalculateSum()
        {
            _3Sum sum = new _3Sum();
            IList <IList <int> > listToTest  = sum.ThreeSum(new int[] { -1, 0, 1, 2, -1, -4 });
            IList <IList <int> > compareList = new List <IList <int> >();

            compareList.Add(new List <int>()
            {
                -1, 0, 1
            });
            compareList.Add(new List <int>()
            {
                -1, -1, 2
            });
            Assert.AreEqual(compareList, listToTest);
        }
Example #6
0
        public void ThreeSum_0000_Test()
        {
            // Arrange
            int[] nums = new int[] { 0, 0, 0, 0 };
            _3Sum algo = new _3Sum(true);

            // Act
            IList <IList <int> > triplets = algo.ThreeSum(nums);

            // Assert
            Assert.AreEqual(1, triplets.Count);

            // 0,0,0
            Assert.AreEqual(0, triplets[0][0]);
            Assert.AreEqual(0, triplets[0][1]);
            Assert.AreEqual(0, triplets[0][2]);
        }
Example #7
0
        public void LeetInput_3()
        {
            // Arrange
            int[] nums = new int[] { 1, -1, -1, 0 };
            _3Sum algo = new _3Sum(true);

            // Act
            IList <IList <int> > triplets = algo.ThreeSum(nums);

            // Assert
            Assert.AreEqual(1, triplets.Count);

            // -1, 0, 1
            Assert.AreEqual(-1, triplets[0][0]);
            Assert.AreEqual(0, triplets[0][1]);
            Assert.AreEqual(1, triplets[0][2]);
        }
Example #8
0
        public void FindTrip_WithNoValidSoln()
        {
            // Arrange
            int[] numbers = new int[] { -2, -1, 0, 6 };
            _3Sum test    = new _3Sum();

            // Act
            try
            {
                List <int[]> actual = test.FindTrip(numbers);
            }
            catch (Exception e)
            {
                // Assert
                StringAssert.Contains(e.Message, "No three numbers in the input array sum to three.");
                return;
            }
        }
Example #9
0
        public void FindTrip_WithValidSoln()
        {
            // Arrange
            int[]        numbers  = new int[] { -2, -1, 0, 1, 2, 3 };
            List <int[]> expected = new List <int[]>
            {
                new int[] { -2, -1, 3 },
                new int[] { -2, 0, 2 },
                new int[] { -1, 0, 1 }
            };
            _3Sum test = new _3Sum();

            // Act
            List <int[]> actual = test.FindTrip(numbers);

            // Assert
            CollectionAssert.AreEqual(expected, actual);
        }
Example #10
0
        public void removeDuplicates_Test()
        {
            // Arrange
            List <IList <int> > set = new List <IList <int> >();

            set.Add(new List <int> {
                -1, 0, 1
            });
            set.Add(new List <int> {
                0, -1, 1
            });
            _3Sum algo = new _3Sum(false);

            // Act
            algo.removeDuplicates(set);

            // Assert
            Assert.AreEqual(1, set.Count);
        }
Example #11
0
        [TestMethod(), Timeout(10000)]  // timeout is in milliseconds
        public void BigArraySize_Test()
        {
            // Arrange
            // using ThreeSumTheSlowWay: 30:8ms, 40:5ms, 100:11ms, 200:180ms, 400:4s, 800:timeout
            int size = 800;     // 100:4ms, 200:4ms, 400:4ms, 800:6ms, 1600:12ms, 3200:31ms, 6400:106ms, 128000:timeout

            int[]  nums = new int[size];
            Random rand = new Random(29322);

            for (int i = 0; i < size; i++)
            {
                nums[i] = rand.Next(-100, 100);
            }
            _3Sum algo = new _3Sum(true);

            // Act
            IList <IList <int> > triplets = algo.ThreeSum(nums);

            // Assert
            Assert.IsTrue(true);    // if the test finishes within the timeout, it passes.
        }
Example #12
0
        public void getTriplets_Test()
        {
            // Arrange
            _3Sum algo = new _3Sum(false);

            int[] nums = { 1, 2, 3, 4, 5 };

            // Act
            List <IList <int> > triplets = algo.getTriplets(nums);

            // Assert
            Assert.AreEqual(10, triplets.Count);

            // 1,2,3
            Assert.AreEqual(1, triplets[0][0]);
            Assert.AreEqual(2, triplets[0][1]);
            Assert.AreEqual(3, triplets[0][2]);

            // 1,2,4
            Assert.AreEqual(1, triplets[1][0]);
            Assert.AreEqual(2, triplets[1][1]);
            Assert.AreEqual(4, triplets[1][2]);

            // 1,2,5
            Assert.AreEqual(1, triplets[2][0]);
            Assert.AreEqual(2, triplets[2][1]);
            Assert.AreEqual(5, triplets[2][2]);

            // 1,3,4
            Assert.AreEqual(1, triplets[3][0]);
            Assert.AreEqual(3, triplets[3][1]);
            Assert.AreEqual(4, triplets[3][2]);

            // 1,3,5
            Assert.AreEqual(1, triplets[4][0]);
            Assert.AreEqual(3, triplets[4][1]);
            Assert.AreEqual(5, triplets[4][2]);

            // 1,4,5
            Assert.AreEqual(1, triplets[5][0]);
            Assert.AreEqual(4, triplets[5][1]);
            Assert.AreEqual(5, triplets[5][2]);

            // 2,3,4
            Assert.AreEqual(2, triplets[6][0]);
            Assert.AreEqual(3, triplets[6][1]);
            Assert.AreEqual(4, triplets[6][2]);

            // 2,3,5
            Assert.AreEqual(2, triplets[7][0]);
            Assert.AreEqual(3, triplets[7][1]);
            Assert.AreEqual(5, triplets[7][2]);

            // 2,4,5
            Assert.AreEqual(2, triplets[8][0]);
            Assert.AreEqual(4, triplets[8][1]);
            Assert.AreEqual(5, triplets[8][2]);

            // 3,4,5
            Assert.AreEqual(3, triplets[9][0]);
            Assert.AreEqual(4, triplets[9][1]);
            Assert.AreEqual(5, triplets[9][2]);
        }