From d87b47b8c09d7bafffbbef91bc2ac57b9af2d934 Mon Sep 17 00:00:00 2001 From: josdem Date: Fri, 19 Apr 2024 17:30:44 -0400 Subject: [PATCH 1/2] Adding half-square runner example --- .../josdem/algorithms/HalfSquareRunner.java | 9 ++++++++ .../algorithms/HalfSquareRunnerTest.java | 22 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 big-o/src/main/java/com/josdem/algorithms/HalfSquareRunner.java create mode 100644 big-o/src/test/java/com/josdem/algorithms/HalfSquareRunnerTest.java diff --git a/big-o/src/main/java/com/josdem/algorithms/HalfSquareRunner.java b/big-o/src/main/java/com/josdem/algorithms/HalfSquareRunner.java new file mode 100644 index 0000000..9a7a57e --- /dev/null +++ b/big-o/src/main/java/com/josdem/algorithms/HalfSquareRunner.java @@ -0,0 +1,9 @@ +package com.josdem.algorithms; + +import java.util.List; + +public class HalfSquareRunner { + public List getTargetNumbers(List numbers, int target) { + return numbers; + } +} diff --git a/big-o/src/test/java/com/josdem/algorithms/HalfSquareRunnerTest.java b/big-o/src/test/java/com/josdem/algorithms/HalfSquareRunnerTest.java new file mode 100644 index 0000000..485499e --- /dev/null +++ b/big-o/src/test/java/com/josdem/algorithms/HalfSquareRunnerTest.java @@ -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 numbers = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); + List expectedNumbers = List.of(0, 5, 1, 4, 2, 3, 3, 2, 4, 1, 5, 0); + int target = 5; + assertEquals(expectedNumbers, halfSquareRunner.getTargetNumbers(numbers, target), "should get numbers from the array"); + } +} From 9c5d2a5746feaeef429288cf7b69d6dc4590b3e2 Mon Sep 17 00:00:00 2001 From: josdem Date: Fri, 19 Apr 2024 18:35:24 -0400 Subject: [PATCH 2/2] Adding half-square runner solution --- .../josdem/algorithms/HalfSquareRunner.java | 21 ++++++++++++++++++- .../josdem/algorithms/SquareTimeRunner.java | 4 ++++ .../algorithms/HalfSquareRunnerTest.java | 2 +- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/big-o/src/main/java/com/josdem/algorithms/HalfSquareRunner.java b/big-o/src/main/java/com/josdem/algorithms/HalfSquareRunner.java index 9a7a57e..783a992 100644 --- a/big-o/src/main/java/com/josdem/algorithms/HalfSquareRunner.java +++ b/big-o/src/main/java/com/josdem/algorithms/HalfSquareRunner.java @@ -1,9 +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 getTargetNumbers(List numbers, int target) { - return numbers; + List 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; } } diff --git a/big-o/src/main/java/com/josdem/algorithms/SquareTimeRunner.java b/big-o/src/main/java/com/josdem/algorithms/SquareTimeRunner.java index 0c78054..4c929ee 100644 --- a/big-o/src/main/java/com/josdem/algorithms/SquareTimeRunner.java +++ b/big-o/src/main/java/com/josdem/algorithms/SquareTimeRunner.java @@ -2,6 +2,7 @@ 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 @@ -9,6 +10,8 @@ public class SquareTimeRunner { + private final Logger log = Logger.getLogger(this.getClass().getName()); + public List getTargetNumbers(List numbers, int target) { List result = new ArrayList<>(); numbers.forEach(a -> { @@ -16,6 +19,7 @@ public List getTargetNumbers(List numbers, int target) { if (a + b == target) { result.add(a); result.add(b); + log.info(a + "," + b); } }); }); diff --git a/big-o/src/test/java/com/josdem/algorithms/HalfSquareRunnerTest.java b/big-o/src/test/java/com/josdem/algorithms/HalfSquareRunnerTest.java index 485499e..80e97ea 100644 --- a/big-o/src/test/java/com/josdem/algorithms/HalfSquareRunnerTest.java +++ b/big-o/src/test/java/com/josdem/algorithms/HalfSquareRunnerTest.java @@ -15,7 +15,7 @@ class HalfSquareRunnerTest { @DisplayName("show half-square algorithm") void shouldTestHalfSquare(){ List numbers = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); - List expectedNumbers = List.of(0, 5, 1, 4, 2, 3, 3, 2, 4, 1, 5, 0); + List expectedNumbers = List.of(0, 5, 1, 4, 2, 3); int target = 5; assertEquals(expectedNumbers, halfSquareRunner.getTargetNumbers(numbers, target), "should get numbers from the array"); }