Beispiel #1
0
        public void RotateListElements_ShouldRotateList_WhenPassedNumHigherThanCount()
        {
            // arrange
            var original = new List <int> {
                1, 2, 3
            };
            int numRotations = 5;
            var expected     = new List <int> {
                2, 3, 1
            };

            var original5 = new List <int> {
                1, 2, 3, 4, 5
            };
            int numRotations5 = 13;
            var expected5     = new List <int> {
                3, 4, 5, 1, 2
            };

            var x = new RotateArray();

            // act
            var actual  = x.RotateListElements(original, numRotations);
            var actual5 = x.RotateListElements(original5, numRotations5);

            // assert
            Assert.Equal(expected, actual);
            Assert.Equal(expected5, actual5);
        }
        public static void MainMethod()
        {
            int[]       nums = new int[] { 1, 2, 3, 4 };
            RotateArray obj  = new RotateArray();

            obj.Rotate(nums, 2);
        }
Beispiel #3
0
        public void RotateListElements_ShouldReturnListRotatedOnce_WhenPassedListAndASingleRotation()
        {
            // arrange
            var original = new List <int>()
            {
                1, 2, 3
            };
            int numRotations = 1;
            var expected     = new List <int>()
            {
                3, 1, 2
            };

            var original5 = new List <int>()
            {
                1, 2, 3, 4, 5
            };
            int numRotations5 = 3;
            var expected5     = new List <int> {
                3, 4, 5, 1, 2
            };

            var x = new RotateArray();

            // act
            var actual  = x.RotateListElements(original, numRotations);
            var actual5 = x.RotateListElements(original5, numRotations5);

            // assert
            Assert.Equal(expected, actual);
            Assert.Equal(expected5, actual5);
        }
Beispiel #4
0
        public void RotateListElements_WhenPassedIntListAndNegNumLargerThanListCount_ShouldReturnListRotated()
        {
            // arrange
            var original = new List <int>()
            {
                1, 2, 3
            };
            int numRotations = -4;
            var expected     = new List <int>()
            {
                2, 3, 1
            };

            var original6 = new List <int>()
            {
                1, 2, 3, 4, 5, 6
            };
            int numRotations6 = -16;
            var expected6     = new List <int>()
            {
                5, 6, 1, 2, 3, 4
            };

            var x = new RotateArray();

            // act
            var actual  = x.RotateListElements(original, numRotations);
            var actual6 = x.RotateListElements(original6, numRotations6);

            // assert
            Assert.Equal(expected, actual);
            Assert.Equal(expected, actual);
        }
        public void TwoElem()
        {
            var nums = new[] { 1, 2 };

            var sol = new RotateArray();

            sol.Rotate(nums, 0);

            CollectionAssert.AreEqual(new[] { 1, 2 }, nums);

            sol.Rotate(nums, 1);

            CollectionAssert.AreEqual(new[] { 2, 1 }, nums);

            sol.Rotate(nums, 2);

            CollectionAssert.AreEqual(new[] { 2, 1 }, nums);

            sol.Rotate(nums, 3);

            CollectionAssert.AreEqual(new[] { 1, 2 }, nums);

            sol.Rotate(nums, 4);

            CollectionAssert.AreEqual(new[] { 1, 2 }, nums);
        }
        public void ThreeElem()
        {
            var nums = new[] { 1, 2, 3 };

            var sol = new RotateArray();

            sol.Rotate(nums, 0);

            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, nums);

            sol.Rotate(nums, 1);

            CollectionAssert.AreEqual(new[] { 3, 1, 2 }, nums);

            sol.Rotate(nums, 2);

            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, nums);

            sol.Rotate(nums, 3);

            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, nums);

            sol.Rotate(nums, 4);

            CollectionAssert.AreEqual(new[] { 3, 1, 2 }, nums);

            sol.Rotate(nums, 5);

            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, nums);
        }
        public void Example1010Elems()
        {
            var nums = Enumerable
                       .Repeat(0, 1010)
                       .Select((z, i) => i)
                       .ToArray();

            var sol = new RotateArray();

            for (int i = 0; i < nums.Length * 2; i++)
            {
                var array = nums.Clone() as int[];

                var expected = new List <int>(nums.Clone() as int[]);

                var k = i % nums.Length;

                sol.Rotate(array, i);

                for (int j = 0; j < k; j++)
                {
                    var n = expected[expected.Count - 1];

                    expected.RemoveAt(expected.Count - 1);

                    expected.Insert(0, n);
                }

                CollectionAssert.AreEqual(expected, array, $"i = {i}");
            }
        }
Beispiel #8
0
        public void RotateListElements_ShouldReturnListRotatedToLeft_WhenPassedNegativeRotationNum()
        {
            // arrange
            var original = new List <int>()
            {
                1, 2, 3
            };
            int numRotations = -1;
            var expected     = new List <int>()
            {
                2, 3, 1
            };

            var original5 = new List <int>()
            {
                1, 2, 3, 4, 5
            };
            int numRotations5 = -3;
            var expected5     = new List <int>()
            {
                4, 5, 1, 2, 3
            };

            var x = new RotateArray();

            // act
            var actual  = x.RotateListElements(original, numRotations);
            var actual5 = x.RotateListElements(original5, numRotations5);

            // assert
            Assert.Equal(expected, actual);
            Assert.Equal(expected5, actual5);
        }
Beispiel #9
0
        public void TestRotateArray1()
        {
            var arr = new [] { 1, 2, 3, 4, 5, 6, 7 };

            RotateArray.Reserve(arr, 4);

            Assert.AreEqual(string.Join("", arr), "4567123");
        }
Beispiel #10
0
        public void TestRotateArray2()
        {
            var arr = new[] { 1, 2, 3, 4, 5 };

            RotateArray.Change(arr, 2);

            Assert.AreEqual(string.Join("", arr), "45123");
        }
Beispiel #11
0
        public void RotateArrayTests()
        {
            var nums   = new[] { 1, 2, 3, 4, 5, 6, 7 };
            var k      = 3;
            var solver = new RotateArray();

            solver.Solution(nums, k);
            Assert.AreEqual(nums, new [] { 5, 6, 7, 1, 2, 3, 4 });
        }
Beispiel #12
0
        public void SolutionsOneTest()
        {
            int[]       nums = new int[] { 1, 2, 3, 4, 5, 6, 7 };
            int         k    = 3;
            RotateArray myRa = new RotateArray();

            myRa.SolutionsOne(nums, k);
            Assert.Fail();
        }
Beispiel #13
0
        static void Main(string[] args)
        {
            {//Q1 RemoveDuplicates
                /*
                 * int[] nums = new int[]{0,0,1,1,2,2,2,2,3,3,3};
                 * int newLength = RemoveDuplicates(nums);
                 * foreach(int i in nums){
                 *   System.Console.Write(i);
                 * } */
            }

            {//Q2 Best Time to Buy and Sell Stock II
             // MaxProfit_StockMarket solution =  new MaxProfit_StockMarket();
             //   int[] prices = new int[]{7,1,5,3,6,4};//{7,6,4,3,1};//{1,2,3,4,5};//;

                //     int profit = solution.MaxProfit(prices);
            }
            {//Q3  Create Sorted Array through Instructions
             // Output: 26524
             // Expected: 26557
             //    Find_num_of_inversions_in_array solution =  new Find_num_of_inversions_in_array();
             //   int[] prices = new int[]{316,326,221,216,135,383,75,212,244,280,176,323,338,427,193,274,443,
             //   272,284,20,189,403,458,14,372,126,388,157,318,164,317,376,399,384,6,94,142,294,235,166,371,179,
             //   187,77,21,115,455,342,7,346,352,159,405,373,82,232,411,426,137,70,287,98,66,44,153,307,386,312,
             //   139,188,13,15,351,369,185,141,205,328,130,30,358,191,226,209,200,55,74,36,10,385,275,278,2,434,
             //   214,207,359,303,119,334,95,436,450,156,234,168,167,233,46,127,344,453,41,268,315,412,378,273,254,194,
             //   177,395,80,297,64,391,155,110,60,114,117,81,79,39,109,265,367,171,8,140,72,345,314,128,17,299,350,421,
             //   457,444,277,73,283,368,165,174,58,448,393,144,172,146,148,292,361,251,406,92,445,97,18,196,96,101,16,223,
             //   304,332,397,264,452,163,123,35,84,416,348,198,51,290,430,428,68,113,203,263,50,446,336,341,252,211,247,429,
             //   365,5,106,22,43,152,279,225,136,145,231,349,71,99,288,437,298,158,394,415,293,29,289,291,425,63,121,451,4,34,
             //   249,111,59,213,31,201,381,370,362,230,390,353,56,377,204,308,217,408,132,150,88,321,324,364,301,398,319,93,169,133,
             //   266,281,291,134,138,224,285,161,105,236,61,310,202,347,333,242,240,433,183,440,409,107,87,103,32,12,38,86,19,241,89,343,
             //   313,173,327,325,118,441,124,78,442,401,396,354,116,33,379,355,215,76,243,122,454,48,320,257,90,366,417,120,129,295,375,
             //   382,330,182,389,356,387,181,186,238,357,184,53,147,260,37,400,1,25,175,65,131,435,67,210,248,407,62,151,392,49,40,11,439,
             //   335,239,162,410,125,154,250,237,269,28,329,432,423,52,100,306,229,380,255,195,42,23,112,322,206,197,302,24,57,192,300,190,
             //   170,449,108,438,180,222,69,418,363,402,431,267,360,218,404,413,54,422,374,271,27,83,245,104,160,256,102,282,340,
             //   9,424,246,419,47,178,258,337,85,143,339,447,305,26,286,45,219,91,414,311,149,208,276,227,259,456,420,331,199,220,262,309,253,296,3,228,270,261};                 //{1,3,3,3,2,4,2,1,2};//{1,5,6,2};//{1,2,3,6,5,4};//;

                //     int cost = solution.CreateSortedArray(prices);
                //     System.Console.WriteLine("Cost to shift ={0}",cost);
            }
            { //Q4
                // Merge_Arrays m = new Merge_Arrays();
                // int[] Arr1 = new int[]{1,2,3,0,0,0};
                // m.Merge(Arr1, 3,new int[]{2,5,6}, 3);
                // printArray(Arr1);
            }
            {//Q5 RotateArray
                RotateArray R    = new RotateArray();
                int[]       Arr1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 };
                //{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};
                //{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27};
                //{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54};
                R.Rotate(Arr1, 82);
                printArray(Arr1);
            }
        }
Beispiel #14
0
 void InternalTest(int[] nums, int k, int[] expected)
 {
     RotateArray.Rotate(nums, k);
     Assert.Equal <int>(expected.Length, nums.Length);
     for (int i = 0; i < nums.Length; i++)
     {
         Assert.Equal <int>(expected[i], nums[i]);
     }
 }
Beispiel #15
0
        static void Main(string[] args)
        {
            NumberOfIslands number = new NumberOfIslands();

            Console.WriteLine(number.IslandCount());

            SpiralPrint sp = new SpiralPrint();

            sp.Print();

            PascalTriangle pt = new PascalTriangle();

            pt.Generate(6); pt.Print();

            MedianOfSortedArrays ms = new MedianOfSortedArrays();

            Console.WriteLine(ms.MedianOf2SortedArrays(ms.array1, ms.array2));

            FindtheMissingNumber fn = new FindtheMissingNumber();

            Console.WriteLine(fn.FindtheMissingNumberMethod1());

            ContainsDuplicate cd = new ContainsDuplicate();

            Console.WriteLine(cd.isDuplicateMethod2());

            ReversedLinkedList rl = new ReversedLinkedList();

            rl.reversedList();

            ReverseNumber rn = new ReverseNumber();

            rn.ReverseANumber(12345);

            LinearSearch ls = new LinearSearch();
            BinarySearch bs = new BinarySearch();

            ZigZag zz = new ZigZag();

            Console.WriteLine(zz.Convert("PAYPALISHIRING", 3));

            BinaryTree bt = new BinaryTree();

            bt.InorderTraverse(bt.root);
            Console.WriteLine();
            bt.PreorderTraverse(bt.root);
            Console.WriteLine();
            bt.PostorderTraverse(bt.root);

            RotateArray ra = new RotateArray(3);
            //SearchRotatedArray sr = new SearchRotatedArray(3);
            //Console.WriteLine(sr.Find());

            FindSumSarray  fS    = new FindSumSarray();
            MaxSumSubArray mxSum = new MaxSumSubArray();
        }
Beispiel #16
0
        public void RotateTest(int[] nums, int k, int[] expected)
        {
            var sln = new RotateArray();

            sln.Rotate(nums, k);

            for (int i = 0; i < nums.Length; i++)
            {
                Assert.Equal(expected[i], nums[i]);
            }
        }
Beispiel #17
0
        public void TestMethod2(int[] nums, int k, int[] expected)
        {
            // Arrange
            RotateArray question = new RotateArray();

            // Act
            question.Rotate2(nums, k);

            // Assert
            CollectionAssert.AreEqual(expected, nums);
        }
        public void Example2()
        {
            var nums     = new[] { -1, -100, 3, 99 };
            var k        = 2;
            var expected = new[] { 3, 99, -1, -100 };

            var sol = new RotateArray();

            sol.Rotate(nums, k);

            CollectionAssert.AreEqual(expected, nums);
        }
        public void Example1()
        {
            var nums     = new[] { 1, 2, 3, 4, 5, 6, 7 };
            var k        = 3;
            var expected = new[] { 5, 6, 7, 1, 2, 3, 4 };

            var sol = new RotateArray();

            sol.Rotate(nums, k);

            CollectionAssert.AreEqual(expected, nums);
        }
Beispiel #20
0
        public void RotateArray(int[] source, int rotations, int[] expectedResult)
        {
            var target = new RotateArray();
            var result = target.solution(source, rotations);

            Assert.AreEqual(expectedResult.Length, result.Length);

            for (int i = 0; i < expectedResult.Length; i++)
            {
                Assert.AreEqual(expectedResult[i], result[i]);
            }
        }
        public void OneElem()
        {
            var nums     = new[] { 1 };
            var expected = new[] { 1 };

            var sol = new RotateArray();

            for (int i = 0; i < 10; i++)
            {
                sol.Rotate(nums, i);

                CollectionAssert.AreEqual(expected, nums);
            }
        }
Beispiel #22
0
        public void RotateArray_Throw_Exception()
        {
            //arrange
            var moqRotateArray = new RotateArray();
            var expectedResult = new ArgumentNullException();

            //act
            var sut = Assert.Throws <ArgumentNullException>(
                () => moqRotateArray.GetRotateArray(null, -1),
                "should not be null parameters and number should be more than 0");

            //act
            Assert.AreEqual(sut.InnerException, expectedResult.InnerException);
        }
Beispiel #23
0
        public void RotateListElements_ShouldReturnEmptyList_WhenPassedEmptyList()
        {
            // arrange
            var empty        = new List <int>();
            int numRotations = 0;

            var x = new RotateArray();

            // act
            var actual = x.RotateListElements(empty, numRotations);

            // assert
            Assert.Equal(empty, actual);
        }
        public void RotateTests()
        {
            RotateArray obj = new RotateArray();

            var arr = new int[] { 1, 2, 3, 4, 5, 6, 7 };

            obj.Rotate(arr, 3);//[5,6,7,1,2,3,4]

            arr = new int[] { 1, 2, 3, 4, 5, 6, 7 };
            obj.Rotate(arr, 10);//[5,6,7,1,2,3,4]

            arr = new int[] { -1, -100, 3, 99 };
            obj.Rotate(arr, 2);//[3,99,-1,-100]

            arr = new int[] { 1, 2, 3, 4, 5, 6 };
            obj.Rotate(arr, 3);//[4,5,6,1,2,3]
        }
Beispiel #25
0
        public void RotateListElements_ShouldReturnUnchangedList_WhenPassedListAndZeroRotations()
        {
            // arrange
            var original = new List <int>()
            {
                1, 2, 3
            };
            int numRotations = 0;
            var expected     = original;

            var x = new RotateArray();

            // act
            var actual = x.RotateListElements(original, numRotations);

            // assert
            Assert.Equal(expected, actual);
        }
Beispiel #26
0
 public void RotateArray_NegativeKIndex()
 {
     RotateArray.Rotate(new [] { 1 }, -1);
 }
Beispiel #27
0
 public void RotateArray_EmptyArray_Error()
 {
     RotateArray.Rotate(new int[] { }, 1);
 }
Beispiel #28
0
        public static void RotateArray()
        {
            RotateArray rotate = new RotateArray();

            rotate.ReverseArray(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 1, 3);
        }
Beispiel #29
0
        public void Rotate(int[] nums, int k)
        {
            var rotate = new RotateArray(nums, k);

            rotate.PerformRotationInPlace();
        }
Beispiel #30
0
 public void BeforeEach()
 {
     RotateArray = new RotateArray();
 }