Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
josdem committed Apr 19, 2024
2 parents 606327f + 9c5d2a5 commit 4e00a72
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
28 changes: 28 additions & 0 deletions big-o/src/main/java/com/josdem/algorithms/HalfSquareRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.josdem.algorithms;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/* Type: Square Time Algorithms – O(n^2/2)
Description: Half-square increment amount of time based on collection size
*/

public class HalfSquareRunner {
private final Logger log = Logger.getLogger(this.getClass().getName());
public List<Integer> getTargetNumbers(List<Integer> numbers, int target) {
List<Integer> result = new ArrayList<>();
for(int i=0; i < numbers.size(); i++){
for(int j = i + 1; j < numbers.size(); j++){
int a = numbers.get(i);
int b = numbers.get(j);
if (a + b == target){
result.add(a);
result.add(b);
log.info(a + "," + b);
}
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/* Type: Square Time Algorithms – O(n^2)
Description: Square increment amount of time based on collection size
*/

public class SquareTimeRunner {

private final Logger log = Logger.getLogger(this.getClass().getName());

public List<Integer> getTargetNumbers(List<Integer> numbers, int target) {
List<Integer> result = new ArrayList<>();
numbers.forEach(a -> {
numbers.forEach(b -> {
if (a + b == target) {
result.add(a);
result.add(b);
log.info(a + "," + b);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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 HalfSquareRunnerTest {

private final HalfSquareRunner halfSquareRunner = new HalfSquareRunner();

@Test
@DisplayName("show half-square algorithm")
void shouldTestHalfSquare(){
List<Integer> numbers = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
List<Integer> expectedNumbers = List.of(0, 5, 1, 4, 2, 3);
int target = 5;
assertEquals(expectedNumbers, halfSquareRunner.getTargetNumbers(numbers, target), "should get numbers from the array");
}
}

0 comments on commit 4e00a72

Please sign in to comment.