-
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.
Merge remote-tracking branch 'origin/develop'
- Loading branch information
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
big-o/src/main/java/com/josdem/algorithms/HalfSquareRunner.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,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; | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
big-o/src/test/java/com/josdem/algorithms/HalfSquareRunnerTest.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,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"); | ||
} | ||
} |