-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
big-o/src/main/java/com/josdem/algorithms/LargestValueRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> numbers) { | ||
return numbers.stream().filter(it -> it % 2 == 0).max(Integer::compare).orElseThrow(() -> new RuntimeException("No number was found")); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
big-o/src/main/java/com/josdem/algorithms/PrimeNumberRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> getNumber(List<Integer> numbers) { | ||
List<Integer> 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; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
big-o/src/test/java/com/josdem/algorithms/LargestValueRunnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> 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"); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
big-o/src/test/java/com/josdem/algorithms/PrimeNumberRunnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> numbers = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); | ||
List<Integer> expectedNumbers = List.of(2, 3, 5, 7); | ||
assertEquals(expectedNumbers, primeNumberRunner.getNumber(numbers), "should get largest prime number in the array"); | ||
} | ||
} |