Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
josdem committed Apr 20, 2024
2 parents 4e00a72 + 9de1b4c commit 05d88ce
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
14 changes: 14 additions & 0 deletions big-o/src/main/java/com/josdem/algorithms/LargestValueRunner.java
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 big-o/src/main/java/com/josdem/algorithms/PrimeNumberRunner.java
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;
}
}
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");
}
}
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");
}
}

0 comments on commit 05d88ce

Please sign in to comment.