From 0aea7e75962770f84e60ad2a395a4cea0d02f655 Mon Sep 17 00:00:00 2001 From: RicardoRibeiroRodrigues Date: Tue, 20 Aug 2024 14:59:48 -0300 Subject: [PATCH 1/4] Adding the absolute value algorithm --- Algorithms.Tests/Numeric/AbsTests.cs | 46 ++++++++++++++++++ Algorithms/Numeric/Abs.cs | 73 ++++++++++++++++++++++++++++ README.md | 1 + 3 files changed, 120 insertions(+) create mode 100644 Algorithms.Tests/Numeric/AbsTests.cs create mode 100644 Algorithms/Numeric/Abs.cs diff --git a/Algorithms.Tests/Numeric/AbsTests.cs b/Algorithms.Tests/Numeric/AbsTests.cs new file mode 100644 index 00000000..66ba7f99 --- /dev/null +++ b/Algorithms.Tests/Numeric/AbsTests.cs @@ -0,0 +1,46 @@ +using System.Numerics; +using Algorithms.Numeric; +using NUnit.Framework; + +namespace Algorithms.Tests.Numeric; + +public static class AbsTests +{ + [TestCase(0, 0)] + [TestCase(34, 34)] + [TestCase(-100000000000.0d, 100000000000.0d)] + [TestCase(-3, 3)] + [TestCase(-3.1443123d, 3.1443123d)] + public static void GetsAbsVal(T inputNum, T expected) where T : INumber + { + // Act + var result = Abs.AbsVal(inputNum); + + // Assert + Assert.That(result, Is.EqualTo(expected)); + } + + [TestCase(new int[] { -3, -1, 2, -11 }, -11)] + [TestCase(new int[] { 0, 5, 1, 11 }, 11)] + [TestCase(new double[] { 3.0, -10.0, -2.0 }, -10.0d)] + public static void GetAbsMax(T[] inputNums, T expected) where T : INumber + { + // Act + var result = Abs.AbsMax(inputNums); + + // Assert + Assert.That(result, Is.EqualTo(expected)); + } + + [TestCase(new int[] { -3, -1, 2, -11 }, -1)] + [TestCase(new int[] { -3, -5, 1, -11 }, 1)] + [TestCase(new int[] { 0, 5, 1, 11 }, 0)] + public static void GetAbsMin(T[] inputNums, T expected) where T : INumber + { + // Act + var result = Abs.AbsMin(inputNums); + + // Assert + Assert.That(result, Is.EqualTo(expected)); + } +} \ No newline at end of file diff --git a/Algorithms/Numeric/Abs.cs b/Algorithms/Numeric/Abs.cs new file mode 100644 index 00000000..763b6402 --- /dev/null +++ b/Algorithms/Numeric/Abs.cs @@ -0,0 +1,73 @@ +using System; +using System.Numerics; + +namespace Algorithms.Numeric; + +/// +/// Find the absolute value of a number. +/// +public static class Abs +{ + /// + /// Returns the absolute value of a number. + /// + /// Type of number. + /// Number to find the absolute value of. + /// Absolute value of the number. + public static T AbsVal(T inputNum) where T : INumber + { + return T.IsNegative(inputNum) ? -inputNum : inputNum; + } + + /// + /// Returns the number with the smallest absolute value on the input array. + /// + /// Type of number. + /// Array of numbers to find the smallest absolute. + /// Smallest absolute number. + public static T AbsMin(T[] inputNums) where T : INumber + { + if (inputNums.Length == 0) + { + throw new ArgumentException("Array is empty."); + } + + var min = inputNums[0]; + for (var index = 1; index < inputNums.Length; index++) + { + var current = inputNums[index]; + if (AbsVal(current).CompareTo(AbsVal(min)) < 0) + { + min = current; + } + } + + return min; + } + + /// + /// Returns the number with the largest absolute value on the input array. + /// + /// Type of number. + /// Array of numbers to find the largest absolute. + /// Largest absolute number. + public static T AbsMax(T[] inputNums) where T : INumber + { + if (inputNums.Length == 0) + { + throw new ArgumentException("Array is empty."); + } + + var max = inputNums[0]; + for (var index = 1; index < inputNums.Length; index++) + { + var current = inputNums[index]; + if (AbsVal(current).CompareTo(AbsVal(max)) > 0) + { + max = current; + } + } + + return max; + } +} diff --git a/README.md b/README.md index 8ed99dc9..6ad84ea3 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ find more than one implementation for the same objective but using different alg * [Extended Euclidean Algorithm](./Algorithms/ModularArithmetic/ExtendedEuclideanAlgorithm.cs) * [Modular Multiplicative Inverse](./Algorithms/ModularArithmetic/ModularMultiplicativeInverse.cs) * [Numeric](./Algorithms/Numeric) + * [Absolute](./Algorithms/Numeric/Abs.cs) * [Aliquot Sum Calculator](./Algorithms/Numeric/AliquotSumCalculator.cs) * [Amicable Numbers Checker](./Algorithms/Numeric/AmicableNumbersChecker.cs) * [Decomposition](./Algorithms/Numeric/Decomposition) From 962102979443aa5d43b6aabf0198bd7562a91bd5 Mon Sep 17 00:00:00 2001 From: RicardoRibeiroRodrigues Date: Tue, 20 Aug 2024 15:24:38 -0300 Subject: [PATCH 2/4] Codacy Static Code Analysis fix --- Algorithms.Tests/Numeric/AbsTests.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Algorithms.Tests/Numeric/AbsTests.cs b/Algorithms.Tests/Numeric/AbsTests.cs index 66ba7f99..e20ed74d 100644 --- a/Algorithms.Tests/Numeric/AbsTests.cs +++ b/Algorithms.Tests/Numeric/AbsTests.cs @@ -20,9 +20,9 @@ public static void GetsAbsVal(T inputNum, T expected) where T : INumber Assert.That(result, Is.EqualTo(expected)); } - [TestCase(new int[] { -3, -1, 2, -11 }, -11)] - [TestCase(new int[] { 0, 5, 1, 11 }, 11)] - [TestCase(new double[] { 3.0, -10.0, -2.0 }, -10.0d)] + [TestCase(new[] { -3, -1, 2, -11 }, -11)] + [TestCase(new[] { 0, 5, 1, 11 }, 11)] + [TestCase(new[] { 3.0, -10.0, -2.0 }, -10.0d)] public static void GetAbsMax(T[] inputNums, T expected) where T : INumber { // Act @@ -32,9 +32,9 @@ public static void GetAbsMax(T[] inputNums, T expected) where T : INumber Assert.That(result, Is.EqualTo(expected)); } - [TestCase(new int[] { -3, -1, 2, -11 }, -1)] - [TestCase(new int[] { -3, -5, 1, -11 }, 1)] - [TestCase(new int[] { 0, 5, 1, 11 }, 0)] + [TestCase(new[] { -3, -1, 2, -11 }, -1)] + [TestCase(new[] { -3, -5, 1, -11 }, 1)] + [TestCase(new[] { 0, 5, 1, 11 }, 0)] public static void GetAbsMin(T[] inputNums, T expected) where T : INumber { // Act From a404c590eab00ee57122c96e187be4d5165979ff Mon Sep 17 00:00:00 2001 From: RicardoRibeiroRodrigues Date: Sun, 25 Aug 2024 15:49:31 -0300 Subject: [PATCH 3/4] Adding tests to uncovered lines Abs.cs --- Algorithms.Tests/Numeric/AbsTests.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Algorithms.Tests/Numeric/AbsTests.cs b/Algorithms.Tests/Numeric/AbsTests.cs index e20ed74d..9cd48f14 100644 --- a/Algorithms.Tests/Numeric/AbsTests.cs +++ b/Algorithms.Tests/Numeric/AbsTests.cs @@ -1,3 +1,4 @@ +using System; using System.Numerics; using Algorithms.Numeric; using NUnit.Framework; @@ -32,6 +33,15 @@ public static void GetAbsMax(T[] inputNums, T expected) where T : INumber Assert.That(result, Is.EqualTo(expected)); } + public static void AbsMaxThrowsArgumentException() + { + // Arrange + var inputNums = Array.Empty(); + + // Assert + Assert.Throws(() => Abs.AbsMax(inputNums)); + } + [TestCase(new[] { -3, -1, 2, -11 }, -1)] [TestCase(new[] { -3, -5, 1, -11 }, 1)] [TestCase(new[] { 0, 5, 1, 11 }, 0)] @@ -43,4 +53,13 @@ public static void GetAbsMin(T[] inputNums, T expected) where T : INumber // Assert Assert.That(result, Is.EqualTo(expected)); } + + public static void AbsMinThrowsArgumentException() + { + // Arrange + var inputNums = Array.Empty(); + + // Assert + Assert.Throws(() => Abs.AbsMin(inputNums)); + } } \ No newline at end of file From 282722e5c431cdef6cfd3929b0dd1f6d559aafce Mon Sep 17 00:00:00 2001 From: RicardoRibeiroRodrigues Date: Sun, 25 Aug 2024 16:08:25 -0300 Subject: [PATCH 4/4] Adding the test atribute --- Algorithms.Tests/Numeric/AbsTests.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Algorithms.Tests/Numeric/AbsTests.cs b/Algorithms.Tests/Numeric/AbsTests.cs index 9cd48f14..8e45bf9b 100644 --- a/Algorithms.Tests/Numeric/AbsTests.cs +++ b/Algorithms.Tests/Numeric/AbsTests.cs @@ -33,6 +33,7 @@ public static void GetAbsMax(T[] inputNums, T expected) where T : INumber Assert.That(result, Is.EqualTo(expected)); } + [Test] public static void AbsMaxThrowsArgumentException() { // Arrange @@ -54,6 +55,7 @@ public static void GetAbsMin(T[] inputNums, T expected) where T : INumber Assert.That(result, Is.EqualTo(expected)); } + [Test] public static void AbsMinThrowsArgumentException() { // Arrange