Пример #1
0
        public void NumArrayTest_2()
        {
            var test = new NumArray(new[] { 9, -8 });

            test.Update(0, 3);
            Assert.AreEqual(-8, test.SumRange(1, 1));
            Assert.AreEqual(-5, test.SumRange(0, 1));
            test.Update(1, -3);
            Assert.AreEqual(0, test.SumRange(0, 1));
        }
Пример #2
0
        public void NumArrayTest_3()
        {
            var test = new NumArray(new[] { -28, -39, 53, 65, 11, -56, -65, -39, -43, 97 });

            Assert.AreEqual(-56 - 65, test.SumRange(5, 6));
            test.Update(9, 27);
            Assert.AreEqual(118, test.SumRange(2, 3));
            Assert.AreEqual(-104, test.SumRange(6, 7));
            test.Update(1, -82);
            test.Update(3, -72);
            Assert.AreEqual(-221, test.SumRange(3, 7));
            Assert.AreEqual(-293, test.SumRange(1, 8));
            test.Update(5, 13);
            test.Update(4, -67);
        }
Пример #3
0
        public void Run()
        {
            var n = new NumArray(new int[] { -1 });

            n.SumRange(0, 0);
            n.Update(0, 1);
            n.SumRange(0, 0);
        }
Пример #4
0
        public void NumArrayTest_1()
        {
            var test = new NumArray(new[] { -1 });

            Assert.AreEqual(-1, test.SumRange(0, 0));
            test.Update(0, 1);
            Assert.AreEqual(1, test.SumRange(0, 0));
        }
Пример #5
0
        public void NumArrayTest()
        {
            var test = new NumArray(new[] { 1, 3, 5 });

            Assert.AreEqual(9, test.SumRange(0, 2));
            test.Update(1, 2);
            Assert.AreEqual(8, test.SumRange(0, 2));
        }
Пример #6
0
        public override void Run(string[] args)
        {
            var narr = new NumArray(new int[] { 1, 3, 5, 9, 4, 6 });

            Debug.Assert(() => narr.SumRange(0, 2), 9);
            Debug.Assert(() => narr.SumRange(0, 5), 28);
            narr.Update(1, 2);
            Debug.Assert(() => narr.SumRange(0, 2), 8);
        }
    private void NumArray_Main(string[] ope, string[] para)
    {
        if (ope.Length != para.Length)
        {
            return;
        }
        if (ope.Length <= 0 || para.Length <= 0)
        {
            return;
        }

        int[] nums = set_array_int(para[0].Split(','));

        NumArray nm = new NumArray(nums);

        for (int n = 0; n < ope.Length; n++)
        {
            if (ope[n] == "NumArray")
            {
                Console.WriteLine("NumArray()");
            }
            else if (ope[n] == "update")
            {
                string[] flds = para[n].Split(',');
                int      i    = int.Parse(flds[0]);
                int      val  = int.Parse(flds[1]);
                nm.Update(i, val);
                Console.WriteLine("update(" + i.ToString()
                                  + ", " + val.ToString()
                                  + ")");
            }
            else if (ope[n] == "sumRange")
            {
                string[] flds = para[n].Split(',');
                int      i    = int.Parse(flds[0]);
                int      j    = int.Parse(flds[1]);
                int      sum  = nm.SumRange(i, j);
                Console.WriteLine("sumRange(" + i.ToString()
                                  + ", " + j.ToString()
                                  + ") = " + sum.ToString());
            }
        }
    }
 public void TestMethod2(int maxLength, int maxValue, int maxOperationCount, int repeatCount)
 {
     Repeat(repeatCount, () =>
     {
         var nums = GenerateIntegerArray(maxLength, maxValue);
         var numArray = new NumArray(nums);
         var numsCopy = nums.ToArray();
         var operationCount = nums.Any() ? Random.Next(maxOperationCount) + 1 : 0;
         //System.Console.WriteLine(JsonConvert.SerializeObject(numsCopy));
         for (var i = 0; i < operationCount; ++i)
         {
             switch(Random.Next(2))
             {
                 case 0:
                     var index = Random.Next(nums.Length);
                     var newValue = Random.Next(maxValue + 1);
                     numArray.Update(index, newValue);
                     numsCopy[index] = newValue;
                     //System.Console.WriteLine(JsonConvert.SerializeObject(numsCopy));
                     break;
                 default:
                     var indexI = Random.Next(nums.Length);
                     var indexJ = Random.Next(nums.Length);
                     if (indexI > indexJ)
                     {
                         var t = indexI;
                         indexI = indexJ;
                         indexJ = t;
                     }
                     Assert.AreEqual(Sum(numsCopy, indexI, indexJ), numArray.SumRange(indexI, indexJ),
                     $"i = {indexI} j = {indexJ} nums = {JsonConvert.SerializeObject(numsCopy)}");
                     break;
             }
         }
     });
 }
Пример #9
0
        public static void Run()
        {

            Console.WriteLine("Start: {0}", DateTime.Now);

            int[] nums = new int[] { 1, 2, 3, 4, 5};
            NumArray numArray = new NumArray(nums);
            Console.WriteLine(numArray.SumRange(0, 1));
            Console.WriteLine(numArray.SumRange(1, 2));
            numArray.Update(1, 10);
            Console.WriteLine(numArray.SumRange(1, 2));

            Console.WriteLine("End: {0}", DateTime.Now);

        }