From 2c22117eb2215ebafa5d109418d77ae6e7c4b716 Mon Sep 17 00:00:00 2001 From: Mohammadreza Heidari Date: Thu, 7 Apr 2022 17:27:12 +0300 Subject: [PATCH] chore(CSharp): add minima maxima (#730) Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com> --- algorithms/CSharp/README.md | 1 + algorithms/CSharp/src/Search/minima-maxima.cs | 100 ++++++++++++++++++ .../CSharp/test/Search/minima-maxima.cs | 25 +++++ 3 files changed, 126 insertions(+) create mode 100644 algorithms/CSharp/src/Search/minima-maxima.cs create mode 100644 algorithms/CSharp/test/Search/minima-maxima.cs diff --git a/algorithms/CSharp/README.md b/algorithms/CSharp/README.md index 821ca8b1..4488b595 100644 --- a/algorithms/CSharp/README.md +++ b/algorithms/CSharp/README.md @@ -26,6 +26,7 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/ ## Search - [Binary Search](src/Search/binary-search.cs) - [Linear Search](src/Search/linear-search.cs) +- [Minima Maxima](src/Search/minima-maxima.cs) ## Maths - [Abundant Number](src/Maths/abundant-number.cs) diff --git a/algorithms/CSharp/src/Search/minima-maxima.cs b/algorithms/CSharp/src/Search/minima-maxima.cs new file mode 100644 index 00000000..8d652260 --- /dev/null +++ b/algorithms/CSharp/src/Search/minima-maxima.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Algorithms.Search +{ + public class MinimaMaxima + { + public static void Main() + { + var numbers = new List + { + 3,1,2,5,6,7,4,6,9,10 + }; + + var minimas = FindMinimas(numbers); + var maximas = FindMaximas(numbers); + + foreach (var minima in minimas) + { + Console.WriteLine($"Local Minima: {minima}"); + } + + foreach (var maxima in maximas) + { + Console.WriteLine($"Local Maxima: {maxima}"); + } + } + + public static List FindMinimas(List numbers) + { + var result = new List(); + + if (numbers.Count < 3) + { + result.Add(numbers.Min()); + } + else + { + // Check first element + if (numbers[0] < numbers[1]) + { + result.Add(numbers[0]); + } + + //Loop middle elements + for (int i = 1; i < numbers.Count - 1; i++) + { + if (numbers[i - 1] >= numbers[i] && numbers[i] <= numbers[i + 1]) + { + result.Add(numbers[i]); + } + } + + //Check last elements + if (numbers[^1] < numbers[^2]) + { + result.Add(numbers[^1]); + } + } + + return result; + } + + public static List FindMaximas(List numbers) + { + var result = new List(); + + if (numbers.Count < 3) + { + result.Add(numbers.Max()); + } + else + { + // Check first element + if (numbers[0] > numbers[1]) + { + result.Add(numbers[0]); + } + + //Loop middle elements + for (int i = 1; i < numbers.Count - 1; i++) + { + if (numbers[i - 1] <= numbers[i] && numbers[i] >= numbers[i + 1]) + { + result.Add(numbers[i]); + } + } + + //Check last elements + if (numbers[^1] > numbers[^2]) + { + result.Add(numbers[^1]); + } + } + + return result; + } + } +} diff --git a/algorithms/CSharp/test/Search/minima-maxima.cs b/algorithms/CSharp/test/Search/minima-maxima.cs new file mode 100644 index 00000000..3c4bdc88 --- /dev/null +++ b/algorithms/CSharp/test/Search/minima-maxima.cs @@ -0,0 +1,25 @@ +using Algorithms.Search; +using NUnit.Framework; +using System.Linq; + +namespace Algorithms.Tests.Search +{ + [TestFixture] + public class MinimaMaximaTest + { + + [TestCase(new int[] { 3, 1, 2, 5, 6, 7, 4, 6, 9, 10 }, new int[] { 1, 4 })] + public void PassIntegerList_ShouldGetExpectedMinimas(int[] input, int[] result) + { + var expected1 = MinimaMaxima.FindMinimas(input.ToList()); + Assert.AreEqual(expected1, result); + } + + [TestCase(new int[] { 3, 1, 2, 5, 6, 7, 4, 6, 9, 10 }, new int[] { 3, 7, 10 })] + public void PassIntegerList_ShouldGetExpectedMaximas(int[] input, int[] result) + { + var expected1 = MinimaMaxima.FindMaximas(input.ToList()); + Assert.AreEqual(expected1, result); + } + } +}