mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 01:35:16 +08:00
@ -2,35 +2,31 @@ package com.thealgorithms.maths;
|
|||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
|
||||||
* description:
|
|
||||||
*
|
|
||||||
* <p>
|
|
||||||
* absMin([0, 5, 1, 11]) = 0, absMin([3 , -10, -2]) = -2
|
|
||||||
*/
|
|
||||||
public class AbsoluteMin {
|
public class AbsoluteMin {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
int[] testnums = {4, 0, 16};
|
|
||||||
assert absMin(testnums) == 0;
|
|
||||||
|
|
||||||
int[] numbers = {3, -10, -2};
|
|
||||||
System.out.println("absMin(" + Arrays.toString(numbers) + ") = " + absMin(numbers));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the value, returns the absolute min value min
|
* Compares the numbers given as arguments to get the absolute min value.
|
||||||
*
|
*
|
||||||
* @param numbers contains elements
|
* @param numbers The numbers to compare
|
||||||
* @return the absolute min value
|
* @return The absolute min value
|
||||||
*/
|
*/
|
||||||
public static int absMin(int[] numbers) {
|
public static int getMinValue(int... numbers) {
|
||||||
int absMinValue = numbers[0];
|
if (numbers.length == 0) {
|
||||||
for (int i = 1, length = numbers.length; i < length; ++i) {
|
throw new IllegalArgumentException("Numbers array cannot be empty");
|
||||||
if (Math.abs(numbers[i]) < Math.abs(absMinValue)) {
|
|
||||||
absMinValue = numbers[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return absMinValue;
|
|
||||||
|
var absMinWrapper = new Object() {
|
||||||
|
int value = numbers[0];
|
||||||
|
};
|
||||||
|
|
||||||
|
Arrays.stream(numbers)
|
||||||
|
.skip(1)
|
||||||
|
.forEach(number -> {
|
||||||
|
if (Math.abs(number) < Math.abs(absMinWrapper.value)) {
|
||||||
|
absMinWrapper.value = number;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return absMinWrapper.value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
21
src/test/java/com/thealgorithms/maths/AbsoluteMinTest.java
Normal file
21
src/test/java/com/thealgorithms/maths/AbsoluteMinTest.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package com.thealgorithms.maths;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
public class AbsoluteMinTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetMinValue() {
|
||||||
|
assertEquals(0, AbsoluteMin.getMinValue(4, 0, 16));
|
||||||
|
assertEquals(-2, AbsoluteMin.getMinValue(3, -10, -2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetMinValueWithNoArguments() {
|
||||||
|
Exception exception = assertThrows(IllegalArgumentException.class, () -> AbsoluteMin.getMinValue());
|
||||||
|
assertEquals("Numbers array cannot be empty", exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user