diff --git a/algorithms-genetic/src/main/java/com/baeldung/algorithms/RunAlgorithm.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/RunAlgorithm.java index 133c58ce619b..779cb9b97001 100644 --- a/algorithms-genetic/src/main/java/com/baeldung/algorithms/RunAlgorithm.java +++ b/algorithms-genetic/src/main/java/com/baeldung/algorithms/RunAlgorithm.java @@ -1,11 +1,11 @@ package com.baeldung.algorithms; +import java.util.Scanner; + import com.baeldung.algorithms.ga.annealing.SimulatedAnnealing; import com.baeldung.algorithms.ga.ant_colony.AntColonyOptimization; import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm; -import java.util.Scanner; - public class RunAlgorithm { public static void main(String[] args) throws InstantiationException, IllegalAccessException { diff --git a/algorithms-genetic/src/test/java/com/baeldung/algorithms/AntColonyOptimizationLongRunningUnitTest.java b/algorithms-genetic/src/test/java/com/baeldung/algorithms/AntColonyOptimizationLongRunningUnitTest.java index 24f6d044966f..2ac7adc3aa66 100644 --- a/algorithms-genetic/src/test/java/com/baeldung/algorithms/AntColonyOptimizationLongRunningUnitTest.java +++ b/algorithms-genetic/src/test/java/com/baeldung/algorithms/AntColonyOptimizationLongRunningUnitTest.java @@ -1,9 +1,9 @@ package com.baeldung.algorithms; -import com.baeldung.algorithms.ga.ant_colony.AntColonyOptimization; import org.junit.Assert; import org.junit.Test; +import com.baeldung.algorithms.ga.ant_colony.AntColonyOptimization; public class AntColonyOptimizationLongRunningUnitTest { diff --git a/algorithms-genetic/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmLongRunningUnitTest.java b/algorithms-genetic/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmLongRunningUnitTest.java index 57a3e33e4f72..e819da4b36d4 100644 --- a/algorithms-genetic/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmLongRunningUnitTest.java +++ b/algorithms-genetic/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmLongRunningUnitTest.java @@ -1,9 +1,9 @@ package com.baeldung.algorithms; -import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm; import org.junit.Assert; import org.junit.Test; +import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm; public class BinaryGeneticAlgorithmLongRunningUnitTest { diff --git a/algorithms-genetic/src/test/java/com/baeldung/algorithms/SimulatedAnnealingLongRunningUnitTest.java b/algorithms-genetic/src/test/java/com/baeldung/algorithms/SimulatedAnnealingLongRunningUnitTest.java index c5f7e154e210..2ce7d75e436b 100644 --- a/algorithms-genetic/src/test/java/com/baeldung/algorithms/SimulatedAnnealingLongRunningUnitTest.java +++ b/algorithms-genetic/src/test/java/com/baeldung/algorithms/SimulatedAnnealingLongRunningUnitTest.java @@ -1,9 +1,9 @@ package com.baeldung.algorithms; -import com.baeldung.algorithms.ga.annealing.SimulatedAnnealing; import org.junit.Assert; import org.junit.Test; +import com.baeldung.algorithms.ga.annealing.SimulatedAnnealing; public class SimulatedAnnealingLongRunningUnitTest { diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/ApacheCommonsCombinationGenerator.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/ApacheCommonsCombinationGenerator.java deleted file mode 100644 index 40142ce94079..000000000000 --- a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/ApacheCommonsCombinationGenerator.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.algorithms.combination; - -import java.util.Arrays; -import java.util.Iterator; - -import org.apache.commons.math3.util.CombinatoricsUtils; - -public class ApacheCommonsCombinationGenerator { - - private static final int N = 6; - private static final int R = 3; - - /** - * Print all combinations of r elements from a set - * @param n - number of elements in set - * @param r - number of elements in selection - */ - public static void generate(int n, int r) { - Iterator iterator = CombinatoricsUtils.combinationsIterator(n, r); - while (iterator.hasNext()) { - final int[] combination = iterator.next(); - System.out.println(Arrays.toString(combination)); - } - } - - public static void main(String[] args) { - generate(N, R); - } -} \ No newline at end of file diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/CombinatoricsLibCombinationGenerator.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/CombinatoricsLibCombinationGenerator.java deleted file mode 100644 index 0afdeefb8b27..000000000000 --- a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/CombinatoricsLibCombinationGenerator.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.algorithms.combination; - -import org.paukov.combinatorics3.Generator; - -public class CombinatoricsLibCombinationGenerator { - - public static void main(String[] args) { - Generator.combination(0, 1, 2, 3, 4, 5) - .simple(3) - .stream() - .forEach(System.out::println); - } -} diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/GuavaCombinationsGenerator.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/GuavaCombinationsGenerator.java deleted file mode 100644 index d2783881bacc..000000000000 --- a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/GuavaCombinationsGenerator.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.algorithms.combination; - -import java.util.Arrays; -import java.util.Set; - -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Sets; - -public class GuavaCombinationsGenerator { - - public static void main(String[] args) { - - Set> combinations = Sets.combinations(ImmutableSet.of(0, 1, 2, 3, 4, 5), 3); - System.out.println(combinations.size()); - System.out.println(Arrays.toString(combinations.toArray())); - } -} diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/IterativeCombinationGenerator.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/IterativeCombinationGenerator.java deleted file mode 100644 index 676d2f41e3e2..000000000000 --- a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/IterativeCombinationGenerator.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.baeldung.algorithms.combination; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class IterativeCombinationGenerator { - - private static final int N = 5; - private static final int R = 2; - - /** - * Generate all combinations of r elements from a set - * @param n the number of elements in input set - * @param r the number of elements in a combination - * @return the list containing all combinations - */ - public List generate(int n, int r) { - List combinations = new ArrayList<>(); - int[] combination = new int[r]; - - // initialize with lowest lexicographic combination - for (int i = 0; i < r; i++) { - combination[i] = i; - } - - while (combination[r - 1] < n) { - combinations.add(combination.clone()); - - // generate next combination in lexicographic order - int t = r - 1; - while (t != 0 && combination[t] == n - r + t) { - t--; - } - combination[t]++; - for (int i = t + 1; i < r; i++) { - combination[i] = combination[i - 1] + 1; - } - } - - return combinations; - } - - public static void main(String[] args) { - IterativeCombinationGenerator generator = new IterativeCombinationGenerator(); - List combinations = generator.generate(N, R); - System.out.println(combinations.size()); - for (int[] combination : combinations) { - System.out.println(Arrays.toString(combination)); - } - } -} diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/SelectionRecursiveCombinationGenerator.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/SelectionRecursiveCombinationGenerator.java deleted file mode 100644 index 52305b8c2f4e..000000000000 --- a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/SelectionRecursiveCombinationGenerator.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.baeldung.algorithms.combination; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class SelectionRecursiveCombinationGenerator { - - private static final int N = 6; - private static final int R = 3; - - /** - * Generate all combinations of r elements from a set - * @param n - number of elements in input set - * @param r - number of elements to be chosen - * @return the list containing all combinations - */ - public List generate(int n, int r) { - List combinations = new ArrayList<>(); - helper(combinations, new int[r], 0, n - 1, 0); - return combinations; - } - - /** - * Choose elements from set by recursing over elements selected - * @param combinations - List to store generated combinations - * @param data - current combination - * @param start - starting element of remaining set - * @param end - last element of remaining set - * @param index - number of elements chosen so far. - */ - private void helper(List combinations, int data[], int start, int end, int index) { - if (index == data.length) { - int[] combination = data.clone(); - combinations.add(combination); - } else { - int max = Math.min(end, end + 1 - data.length + index); - for (int i = start; i <= max; i++) { - data[index] = i; - helper(combinations, data, i + 1, end, index + 1); - } - } - } - - public static void main(String[] args) { - SelectionRecursiveCombinationGenerator generator = new SelectionRecursiveCombinationGenerator(); - List combinations = generator.generate(N, R); - for (int[] combination : combinations) { - System.out.println(Arrays.toString(combination)); - } - System.out.printf("generated %d combinations of %d items from %d ", combinations.size(), R, N); - } -} diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/SetRecursiveCombinationGenerator.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/SetRecursiveCombinationGenerator.java deleted file mode 100644 index a73447b31df2..000000000000 --- a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/combination/SetRecursiveCombinationGenerator.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.baeldung.algorithms.combination; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class SetRecursiveCombinationGenerator { - - private static final int N = 5; - private static final int R = 2; - - /** - * Generate all combinations of r elements from a set - * @param n - number of elements in set - * @param r - number of elements in selection - * @return the list containing all combinations - */ - public List generate(int n, int r) { - List combinations = new ArrayList<>(); - helper(combinations, new int[r], 0, n-1, 0); - return combinations; - } - - /** - * @param combinations - List to contain the generated combinations - * @param data - List of elements in the selection - * @param start - index of the starting element in the remaining set - * @param end - index of the last element in the set - * @param index - number of elements selected so far - */ - private void helper(List combinations, int data[], int start, int end, int index) { - if (index == data.length) { - int[] combination = data.clone(); - combinations.add(combination); - } else if (start <= end) { - data[index] = start; - helper(combinations, data, start + 1, end, index + 1); - helper(combinations, data, start + 1, end, index); - } - } - - public static void main(String[] args) { - SetRecursiveCombinationGenerator generator = new SetRecursiveCombinationGenerator(); - List combinations = generator.generate(N, R); - for (int[] combination : combinations) { - System.out.println(Arrays.toString(combination)); - } - System.out.printf("generated %d combinations of %d items from %d ", combinations.size(), R, N); - } -} diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/factorial/Factorial.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/factorial/Factorial.java deleted file mode 100644 index 43d2221773b0..000000000000 --- a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/factorial/Factorial.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.baeldung.algorithms.factorial; - -import java.math.BigInteger; -import java.util.stream.LongStream; - -import org.apache.commons.math3.util.CombinatoricsUtils; - -import com.google.common.math.BigIntegerMath; - -public class Factorial { - - public long factorialUsingForLoop(int n) { - long fact = 1; - for (int i = 2; i <= n; i++) { - fact = fact * i; - } - return fact; - } - - public long factorialUsingStreams(int n) { - return LongStream.rangeClosed(1, n) - .reduce(1, (long x, long y) -> x * y); - } - - public long factorialUsingRecursion(int n) { - if (n <= 2) { - return n; - } - return n * factorialUsingRecursion(n - 1); - } - - private Long[] factorials = new Long[20]; - - public long factorialUsingMemoize(int n) { - - if (factorials[n] != null) { - return factorials[n]; - } - - if (n <= 2) { - return n; - } - long nthValue = n * factorialUsingMemoize(n - 1); - factorials[n] = nthValue; - return nthValue; - } - - public BigInteger factorialHavingLargeResult(int n) { - BigInteger result = BigInteger.ONE; - for (int i = 2; i <= n; i++) - result = result.multiply(BigInteger.valueOf(i)); - return result; - } - - public long factorialUsingApacheCommons(int n) { - return CombinatoricsUtils.factorial(n); - } - - public BigInteger factorialUsingGuava(int n) { - return BigIntegerMath.factorial(n); - } - -} diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/montecarlo/MonteCarloTreeSearch.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/montecarlo/MonteCarloTreeSearch.java index 9835e5f53470..a4918f674dbd 100644 --- a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/montecarlo/MonteCarloTreeSearch.java +++ b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/montecarlo/MonteCarloTreeSearch.java @@ -2,9 +2,9 @@ import java.util.List; +import com.baeldung.algorithms.mcts.tictactoe.Board; import com.baeldung.algorithms.mcts.tree.Node; import com.baeldung.algorithms.mcts.tree.Tree; -import com.baeldung.algorithms.mcts.tictactoe.Board; public class MonteCarloTreeSearch { diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/montecarlo/UCT.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/montecarlo/UCT.java index b345bbec09b2..52707aab55e6 100644 --- a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/montecarlo/UCT.java +++ b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/montecarlo/UCT.java @@ -2,6 +2,7 @@ import java.util.Collections; import java.util.Comparator; +import java.util.List; import com.baeldung.algorithms.mcts.tree.Node; diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/tictactoe/Board.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/tictactoe/Board.java index 1402c5e22dba..5ca2d626f1f8 100644 --- a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/tictactoe/Board.java +++ b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/tictactoe/Board.java @@ -1,6 +1,7 @@ package com.baeldung.algorithms.mcts.tictactoe; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class Board { diff --git a/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/binarysearch/BinarySearchUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/binarysearch/BinarySearchUnitTest.java index 2855c5667da4..826682d373d0 100644 --- a/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/binarysearch/BinarySearchUnitTest.java +++ b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/binarysearch/BinarySearchUnitTest.java @@ -5,6 +5,7 @@ import org.junit.Assert; import org.junit.Test; +import com.baeldung.algorithms.binarysearch.BinarySearch; public class BinarySearchUnitTest { diff --git a/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/combination/CombinationUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/combination/CombinationUnitTest.java deleted file mode 100644 index 987b6ddae640..000000000000 --- a/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/combination/CombinationUnitTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.baeldung.algorithms.combination; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.List; - -import org.junit.Test; - -public class CombinationUnitTest { - - private static final int N = 5; - private static final int R = 3; - private static final int nCr = 10; - - @Test - public void givenSetAndSelectionSize_whenCalculatedUsingSetRecursiveAlgorithm_thenExpectedCount() { - SetRecursiveCombinationGenerator generator = new SetRecursiveCombinationGenerator(); - List selection = generator.generate(N, R); - assertEquals(nCr, selection.size()); - } - - @Test - public void givenSetAndSelectionSize_whenCalculatedUsingSelectionRecursiveAlgorithm_thenExpectedCount() { - SelectionRecursiveCombinationGenerator generator = new SelectionRecursiveCombinationGenerator(); - List selection = generator.generate(N, R); - assertEquals(nCr, selection.size()); - } - - @Test - public void givenSetAndSelectionSize_whenCalculatedUsingIterativeAlgorithm_thenExpectedCount() { - IterativeCombinationGenerator generator = new IterativeCombinationGenerator(); - List selection = generator.generate(N, R); - assertEquals(nCr, selection.size()); - } -} diff --git a/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/factorial/FactorialUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/factorial/FactorialUnitTest.java deleted file mode 100644 index c185dba62bc7..000000000000 --- a/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/factorial/FactorialUnitTest.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.baeldung.algorithms.factorial; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.math.BigInteger; - -import org.junit.Before; -import org.junit.Test; - -public class FactorialUnitTest { - - Factorial factorial; - - @Before - public void setup() { - factorial = new Factorial(); - } - - @Test - public void whenCalculatingFactorialUsingForLoop_thenCorrect() { - int n = 5; - - assertThat(factorial.factorialUsingForLoop(n)).isEqualTo(120); - } - - @Test - public void whenCalculatingFactorialUsingStreams_thenCorrect() { - int n = 5; - - assertThat(factorial.factorialUsingStreams(n)).isEqualTo(120); - } - - @Test - public void whenCalculatingFactorialUsingRecursion_thenCorrect() { - int n = 5; - - assertThat(factorial.factorialUsingRecursion(n)).isEqualTo(120); - } - - @Test - public void whenCalculatingFactorialUsingMemoize_thenCorrect() { - int n = 5; - - assertThat(factorial.factorialUsingMemoize(n)).isEqualTo(120); - - n = 6; - - assertThat(factorial.factorialUsingMemoize(n)).isEqualTo(720); - } - - @Test - public void whenCalculatingFactorialHavingLargeResult_thenCorrect() { - int n = 22; - - assertThat(factorial.factorialHavingLargeResult(n)).isEqualTo(new BigInteger("1124000727777607680000")); - } - - @Test - public void whenCalculatingFactorialUsingApacheCommons_thenCorrect() { - int n = 5; - - assertThat(factorial.factorialUsingApacheCommons(n)).isEqualTo(120); - } - - @Test - public void whenCalculatingFactorialUsingGuava_thenCorrect() { - int n = 22; - - assertThat(factorial.factorialUsingGuava(n)).isEqualTo(new BigInteger("1124000727777607680000")); - } - -} diff --git a/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/minimax/MinimaxUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/minimax/MinimaxUnitTest.java index 070e29f30c87..59f0fcf0536f 100644 --- a/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/minimax/MinimaxUnitTest.java +++ b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/minimax/MinimaxUnitTest.java @@ -4,6 +4,8 @@ import org.junit.Test; import static org.junit.Assert.*; +import com.baeldung.algorithms.minimax.MiniMax; +import com.baeldung.algorithms.minimax.Tree; public class MinimaxUnitTest { private Tree gameTree; diff --git a/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/conversion/ByteArrayConverterUnitTest.java b/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/conversion/ByteArrayConverterUnitTest.java index 7bb57ca07ff5..be6180270563 100644 --- a/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/conversion/ByteArrayConverterUnitTest.java +++ b/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/conversion/ByteArrayConverterUnitTest.java @@ -1,6 +1,7 @@ package com.baeldung.algorithms.conversion; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import org.apache.commons.codec.DecoderException; @@ -8,6 +9,7 @@ import org.junit.Before; import org.junit.Test; +import com.baeldung.algorithms.conversion.HexStringConverter; public class ByteArrayConverterUnitTest { diff --git a/algorithms-miscellaneous-2/src/test/resources/graph.png b/algorithms-miscellaneous-2/src/test/resources/graph.png index c4b7531ee913..7165a517824f 100644 Binary files a/algorithms-miscellaneous-2/src/test/resources/graph.png and b/algorithms-miscellaneous-2/src/test/resources/graph.png differ diff --git a/java-math/pom.xml b/java-math/pom.xml index a009116685b9..159d053df33a 100644 --- a/java-math/pom.xml +++ b/java-math/pom.xml @@ -58,26 +58,6 @@ - - - - org.codehaus.mojo - cobertura-maven-plugin - 2.7 - - - - com/baeldung/math/dijkstra/* - - - com/baeldung/math/dijkstra/* - - - - - - - 3.6.1 3.9.0