From 816a369a5482faf678e5d76cff01777d8611dfb7 Mon Sep 17 00:00:00 2001 From: josdem Date: Sun, 21 Apr 2024 08:30:25 -0400 Subject: [PATCH 1/2] Adding plus time example --- .../com/josdem/algorithms/PlusTimeRunner.java | 9 ++++++++ .../josdem/algorithms/PlusTimeRunnerTest.java | 22 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 big-o/src/main/java/com/josdem/algorithms/PlusTimeRunner.java create mode 100644 big-o/src/test/java/com/josdem/algorithms/PlusTimeRunnerTest.java diff --git a/big-o/src/main/java/com/josdem/algorithms/PlusTimeRunner.java b/big-o/src/main/java/com/josdem/algorithms/PlusTimeRunner.java new file mode 100644 index 0000000..2c9d4fc --- /dev/null +++ b/big-o/src/main/java/com/josdem/algorithms/PlusTimeRunner.java @@ -0,0 +1,9 @@ +package com.josdem.algorithms; + +import java.util.List; +public class PlusTimeRunner { + + public List getList() { + return null; + } +} diff --git a/big-o/src/test/java/com/josdem/algorithms/PlusTimeRunnerTest.java b/big-o/src/test/java/com/josdem/algorithms/PlusTimeRunnerTest.java new file mode 100644 index 0000000..fd2c119 --- /dev/null +++ b/big-o/src/test/java/com/josdem/algorithms/PlusTimeRunnerTest.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 PlusTimeRunnerTest { + + private final PlusTimeRunner plusTimeRunner = new PlusTimeRunner(); + + @Test + @DisplayName("show plus time algorithm") + void shouldCreateCollectionWithFixedSize(){ + List numbers = List.of(3, 9, 15, 2, 11, 1, 18, 6, 4, 10); + int expectedSize = 16; + assertEquals(expectedSize, plusTimeRunner.getList().size(), "should have expected size"); + } +} From f28d35ebb9048f1c80a42be4818d86aa3240c654 Mon Sep 17 00:00:00 2001 From: josdem Date: Sun, 21 Apr 2024 08:54:24 -0400 Subject: [PATCH 2/2] Adding plus time runner solution --- .../java/com/josdem/algorithms/PlusTimeRunner.java | 11 +++++++++-- .../com/josdem/algorithms/PlusTimeRunnerTest.java | 6 +++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/big-o/src/main/java/com/josdem/algorithms/PlusTimeRunner.java b/big-o/src/main/java/com/josdem/algorithms/PlusTimeRunner.java index 2c9d4fc..fc5c3e3 100644 --- a/big-o/src/main/java/com/josdem/algorithms/PlusTimeRunner.java +++ b/big-o/src/main/java/com/josdem/algorithms/PlusTimeRunner.java @@ -1,9 +1,16 @@ package com.josdem.algorithms; +import java.util.Arrays; import java.util.List; + +/* Type: Plus Time Algorithms – O(2a + b) + Description: Plus compute time algorithm + */ public class PlusTimeRunner { - public List getList() { - return null; + public List createNewList(List numbers) { + int min = numbers.stream().min(Integer::compare).orElseThrow(() -> new RuntimeException("No min value was found")); + int max = numbers.stream().max(Integer::compare).orElseThrow(() -> new RuntimeException("No max value was found")); + return Arrays.asList(new Integer[max - min + 1]); } } diff --git a/big-o/src/test/java/com/josdem/algorithms/PlusTimeRunnerTest.java b/big-o/src/test/java/com/josdem/algorithms/PlusTimeRunnerTest.java index fd2c119..5fcba87 100644 --- a/big-o/src/test/java/com/josdem/algorithms/PlusTimeRunnerTest.java +++ b/big-o/src/test/java/com/josdem/algorithms/PlusTimeRunnerTest.java @@ -15,8 +15,8 @@ class PlusTimeRunnerTest { @Test @DisplayName("show plus time algorithm") void shouldCreateCollectionWithFixedSize(){ - List numbers = List.of(3, 9, 15, 2, 11, 1, 18, 6, 4, 10); - int expectedSize = 16; - assertEquals(expectedSize, plusTimeRunner.getList().size(), "should have expected size"); + List numbers = List.of(3, 9, 15, 2, 11, 5, 19, 6, 4, 10); + int expectedSize = 18; + assertEquals(expectedSize, plusTimeRunner.createNewList(numbers).size(), "should have expected size"); } }