diff --git a/big-o/src/main/java/com/josdem/algorithms/LargestValueRunner.java b/big-o/src/main/java/com/josdem/algorithms/LargestValueRunner.java new file mode 100644 index 0000000..4036f7f --- /dev/null +++ b/big-o/src/main/java/com/josdem/algorithms/LargestValueRunner.java @@ -0,0 +1,14 @@ +package com.josdem.algorithms; + +import java.util.List; + +/* Type: Largest number in the array Algorithm – O(n * k) + Description: Largest number in the collection + */ + +public class LargestValueRunner { + + public int getNumber(List numbers) { + return numbers.stream().filter(it -> it % 2 == 0).max(Integer::compare).orElseThrow(() -> new RuntimeException("No number was found")); + } +} diff --git a/big-o/src/main/java/com/josdem/algorithms/PrimeNumberRunner.java b/big-o/src/main/java/com/josdem/algorithms/PrimeNumberRunner.java new file mode 100644 index 0000000..d865a18 --- /dev/null +++ b/big-o/src/main/java/com/josdem/algorithms/PrimeNumberRunner.java @@ -0,0 +1,33 @@ +package com.josdem.algorithms; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; + +/* Type: Array size plus compute logic Algorithm – O(a * p) + Description: Collection size plus compute logic of getting largest prime number + */ + +public class PrimeNumberRunner { + + private final Logger log = Logger.getLogger(this.getClass().getName()); + + public List getNumber(List numbers) { + List result = new ArrayList<>(); + numbers.forEach(it -> { + if (isPrime(it)) { + log.info("prime:" + it); + result.add(it); + } + }); + return result; + } + + private boolean isPrime(int number) { + if (number < 2) return false; + for (int i = 2; i < number; i++) { + if (number % i == 0) return false; + } + return true; + } +} diff --git a/big-o/src/test/java/com/josdem/algorithms/LargestValueRunnerTest.java b/big-o/src/test/java/com/josdem/algorithms/LargestValueRunnerTest.java new file mode 100644 index 0000000..e00c160 --- /dev/null +++ b/big-o/src/test/java/com/josdem/algorithms/LargestValueRunnerTest.java @@ -0,0 +1,21 @@ +package com.josdem.algorithms; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class LargestValueRunnerTest { + + private final LargestValueRunner largestValueRunner = new LargestValueRunner(); + + @Test + @DisplayName("show largest value algorithm") + void shouldTestLargestValue(){ + List numbers = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); + int expectedNumber = 8; + assertEquals(expectedNumber, largestValueRunner.getNumber(numbers), "should get largest even value"); + } +} diff --git a/big-o/src/test/java/com/josdem/algorithms/PrimeNumberRunnerTest.java b/big-o/src/test/java/com/josdem/algorithms/PrimeNumberRunnerTest.java new file mode 100644 index 0000000..3b1c166 --- /dev/null +++ b/big-o/src/test/java/com/josdem/algorithms/PrimeNumberRunnerTest.java @@ -0,0 +1,21 @@ +package com.josdem.algorithms; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class PrimeNumberRunnerTest { + + private final PrimeNumberRunner primeNumberRunner = new PrimeNumberRunner(); + + @Test + @DisplayName("show array plus compute logic algorithm") + void shouldReturnLargestPrimeNumber(){ + List numbers = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); + List expectedNumbers = List.of(2, 3, 5, 7); + assertEquals(expectedNumbers, primeNumberRunner.getNumber(numbers), "should get largest prime number in the array"); + } +}