Example #1
0
        public void Test3()
        {
            var mf = new MedianFinder();

            mf.AddNum(1);
            mf.FindMedian().Should().Be(1);
            mf.AddNum(2);
            mf.FindMedian().Should().Be(1.5);
            mf.AddNum(3);
            mf.FindMedian().Should().Be(2);
            mf.AddNum(4);
            mf.FindMedian().Should().Be(2.5);
            mf.AddNum(5);
            mf.FindMedian().Should().Be(3);
            mf.AddNum(6);
            mf.FindMedian().Should().Be(3.5);
            mf.AddNum(7);
            mf.FindMedian().Should().Be(4);
            mf.AddNum(8);
            mf.FindMedian().Should().Be(4.5);
            mf.AddNum(9);
            mf.FindMedian().Should().Be(5);
            mf.AddNum(10);
            mf.FindMedian().Should().Be(5.5);
        }
Example #2
0
        public void TestMedian()
        {
            var medianFinder = new MedianFinder();

            medianFinder.AddNum(1);

            Assert.Equal <double>(1.0, medianFinder.FindMedian());

            medianFinder.AddNum(2);
            medianFinder.AddNum(3);
            medianFinder.AddNum(4);

            Assert.Equal <double>(2.5, medianFinder.FindMedian());

            medianFinder.AddNum(5);

            Assert.Equal <double>(3.0, medianFinder.FindMedian());

            medianFinder.AddNum(6);
            medianFinder.AddNum(6);
            medianFinder.AddNum(6);
            medianFinder.AddNum(7);
            medianFinder.AddNum(8);

            Assert.Equal <double>(5.5, medianFinder.FindMedian());
        }
Example #3
0
        public void Test1()
        {
            var mf = new MedianFinder();

            mf.AddNum(1);
            mf.AddNum(2);
            mf.FindMedian().Should().Be(1.5);
        }
Example #4
0
        public void Test2()
        {
            var mf = new MedianFinder();

            mf.AddNum(1);
            mf.AddNum(2);
            mf.AddNum(3);
            mf.AddNum(4);
            mf.AddNum(5);
            mf.FindMedian().Should().Be(3);
        }
        public static void Run()
        {
            var instance = new MedianFinder();

            instance.AddNum(-1);
            Console.WriteLine(instance.FindMedian());
            instance.AddNum(-2);
            Console.WriteLine(instance.FindMedian());
            instance.AddNum(-3);
            Console.WriteLine(instance.FindMedian());
        }
        public void Example2()
        {
            var finder = new MedianFinder();

            var values = new[]
            {
                40, 12, 16, 14, 35, 19, 34, 35, 28, 35, 26, 6, 8, 2, 14, 25, 25, 4, 33, 18, 10, 14,
                27, 3, 35, 13, 24, 27, 14, 5, 0, 38, 19, 25, 11, 14, 31, 30, 11, 31, 0
            };

            var medians = new[]
            {
                40.00000, 26.00000, 16.00000, 15.00000, 16.00000, 17.50000, 19.00000,
                26.50000, 28.00000, 31.00000, 28.00000, 27.00000, 26.00000, 22.50000,
                19.00000, 22.00000, 25.00000, 22.00000, 25.00000, 22.00000, 19.00000,
                18.50000, 19.00000, 18.50000, 19.00000, 18.50000, 19.00000, 21.50000,
                19.00000, 18.50000, 18.00000, 18.50000, 19.00000, 19.00000, 19.00000,
                18.50000, 19.00000, 19.00000, 19.00000, 19.00000, 19.00000
            };

            Assert.AreEqual(values.Length, medians.Length);

            for (int i = 0; i < values.Length; i++)
            {
                var val    = values[i];
                var median = medians[i];

                finder.AddNum(val);

                Assert.AreEqual(median, finder.FindMedian(), $"index {i}");
            }
        }
Example #7
0
        public void MedianFinderBasic()
        {
            var medianFinder = new MedianFinder();

            medianFinder.AddNum(1);
            medianFinder.AddNum(2);

            Assert.Equal(1.5, medianFinder.FindMedian());

            medianFinder.AddNum(2);
            Assert.Equal(2, medianFinder.FindMedian());

            medianFinder.AddNum(1);
            medianFinder.AddNum(1);
            Assert.Equal(1, medianFinder.FindMedian());
        }
 public void TestMethod(int maxOpsCount, int repeatTimes)
 {
     Repeat(repeatTimes, () => {
         var opsCount = Random.Next(maxOpsCount + 1);
         var list = new SortedList<Tuple<double, int>, object>(new Comparer());
         var finder = new MedianFinder();
         for (var i = 0; i < opsCount; ++i)
         {
             if (i == 0 || Random.Next(2) == 0)
             {
                 double newValue = Random.Next(100);
                 //System.Console.WriteLine("Add: " + newValue);
                 list.Add(Tuple.Create(newValue, i), null);
                 finder.AddNum(newValue);
             }
             else
             {
                 double expectedMedian;
                 if (list.Count % 2 == 0)
                 {
                     var x = new SortedList<object, object>();
                     expectedMedian = (list.ElementAt(list.Count / 2 - 1).Key.Item1 + list.ElementAt(list.Count / 2).Key.Item1) / 2;
                 }
                 else
                 {
                     expectedMedian = list.ElementAt(list.Count / 2).Key.Item1;
                 }
                 if (expectedMedian != finder.FindMedian())
                 {
                     Assert.AreEqual(expectedMedian, finder.FindMedian(), JsonConvert.SerializeObject(list.Keys.Select(k => k.Item1)));
                 }
             }
         }
     });
 }
        public void Test()
        {
            // Arrange
            var sol = new MedianFinder();

            sol.AddNum(-1);
            sol.AddNum(-2);
            sol.AddNum(-3);
            sol.AddNum(-4);
            sol.AddNum(-5);

            // Act
            var data = sol.FindMedian();

            // Assert
            Assert.AreEqual(-3, data);
        }
Example #10
0
    public void Run()
    {
        MedianFinder obj = new MedianFinder();

/*         obj.AddNum(1);
 *      obj.AddNum(2);
 *      Console.WriteLine(obj.FindMedian());// -> 1.5
 *      obj.AddNum(3) ;
 *      Console.WriteLine(obj.FindMedian()); // -> 2 */

        obj.AddNum(-1);
        Console.WriteLine(obj.FindMedian());
        obj.AddNum(-2);
        Console.WriteLine(obj.FindMedian()); // -> 2
        obj.AddNum(-3);
        Console.WriteLine(obj.FindMedian()); // -> 2
        obj.AddNum(-4);
        Console.WriteLine(obj.FindMedian()); // -> 2
        obj.AddNum(-5);
        Console.WriteLine(obj.FindMedian()); // -> 2
    }
        public void Example1()
        {
            var finder = new MedianFinder();

            finder.AddNum(1);

            Assert.AreEqual(1, finder.FindMedian());

            finder.AddNum(2);

            Assert.AreEqual(1.5, finder.FindMedian());

            finder.AddNum(3);

            Assert.AreEqual(2, finder.FindMedian());

            finder.AddNum(3);

            Assert.AreEqual(2.5, finder.FindMedian());

            finder.AddNum(3);

            Assert.AreEqual(3, finder.FindMedian());

            finder.AddNum(3);

            Assert.AreEqual(3, finder.FindMedian());
        }
Example #12
0
        public double FindMedian(params int[] nums)
        {
            var sut = new MedianFinder();

            foreach (var num in nums)
            {
                sut.AddNum(num);
            }

            var median = sut.FindMedian();

            return(median);
        }
Example #13
0
        public double[] MedianSlidingWindow(int[] nums, int k)
        {
            if (k > nums.Length)
            {
                return(null);
            }
            var medians = new double[nums.Length - k + 1];

            for (int i = 0; i < k; i++)
            {
                _medianFinder.AddNum(nums[i]);
            }

            medians[0] = _medianFinder.FindMedian();

            for (int i = 1, j = k; j < nums.Length; i++, j++)
            {
                _medianFinder.Remove(nums[i - 1]);
                _medianFinder.AddNum(nums[j]);
                medians[i] = _medianFinder.FindMedian();
            }

            return(medians);
        }
        public void Example3()
        {
            var finder = new MedianFinder();

            finder.AddNum(5);

            Assert.AreEqual(5, finder.FindMedianInt());

            finder.AddNum(15);

            Assert.AreEqual(10, finder.FindMedianInt());

            finder.AddNum(1);

            Assert.AreEqual(5, finder.FindMedianInt());

            finder.AddNum(3);

            Assert.AreEqual(4, finder.FindMedianInt());

            finder.AddNum(13);

            Assert.AreEqual(5, finder.FindMedianInt());
        }
Example #15
0
        public void TestMethod1()
        {
            var sut = new MedianFinder();

            sut.AddNum(1);
            sut.AddNum(2);
            Assert.AreEqual(1.5, sut.FindMedian());
            sut.AddNum(3);
            Assert.AreEqual(2.0, sut.FindMedian());
            sut.AddNum(4);
            sut.AddNum(5);
            Assert.AreEqual(3.0, sut.FindMedian());
            sut.AddNum(6);
            Assert.AreEqual(3.5, sut.FindMedian());
            sut.AddNum(6);
            Assert.AreEqual(4.0, sut.FindMedian());
            sut.AddNum(6);
            Assert.AreEqual(4.5, sut.FindMedian());
        }
Example #16
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            MedianFinder medianFinder = new MedianFinder();

            medianFinder.AddNum(1);                       // arr = [1]
            medianFinder.AddNum(2);                       // arr = [1, 2]
            Console.WriteLine(medianFinder.FindMedian()); // return 1.5 (i.e., (1 + 2) / 2)
            medianFinder.AddNum(3);                       // arr[1, 2, 3]
            Console.WriteLine(medianFinder.FindMedian()); // return 2.0
            medianFinder.AddNum(9999);                    // arr[1, 2, 3,9999]
            Console.WriteLine(medianFinder.FindMedian()); // return 2.5
            medianFinder.AddNum(9998);
            medianFinder.AddNum(9997);
            Console.WriteLine(medianFinder.FindMedian());
        }
Example #17
0
        public static void Median()
        {
            MedianFinder median = new MedianFinder();

            median.AddNum(40);
            median.AddNum(12);
            median.AddNum(16);
            median.AddNum(14);
            median.AddNum(35);
            median.AddNum(19);
            median.AddNum(34);
            median.AddNum(35);
            median.AddNum(28);
            median.AddNum(35);
            median.AddNum(26);
            median.AddNum(6);
            median.AddNum(8);
            median.AddNum(2);

            median.FindMedian();

            median.AddNum(14);
            median.AddNum(25);
            median.AddNum(25);
            median.AddNum(4);
            median.AddNum(33);
            median.AddNum(18);
        }