From c9e3413b62b2ca2d2d3a6fbcd10cabecedfebf6f Mon Sep 17 00:00:00 2001 From: marcos-x86 Date: Sun, 3 Sep 2017 10:52:30 -0400 Subject: [PATCH 1/3] Sum of Divided Kata solution implemented --- .../coding/marcos/SumOfDivided.java | 65 +++++++++++++++++++ .../coding/marcos/SumOfDividedTest.java | 47 ++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 src/main/java/org/fundacionjala/coding/marcos/SumOfDivided.java create mode 100644 src/test/java/org/fundacionjala/coding/marcos/SumOfDividedTest.java diff --git a/src/main/java/org/fundacionjala/coding/marcos/SumOfDivided.java b/src/main/java/org/fundacionjala/coding/marcos/SumOfDivided.java new file mode 100644 index 0000000..30c3f0b --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/marcos/SumOfDivided.java @@ -0,0 +1,65 @@ +package org.fundacionjala.coding.marcos; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +/** + * Created by Marcos. + *

+ * Given an array of positive or negative integers + * I= [i1,..,in] you have to produce a sorted array P of the form + * [ [p, sum of all ij of I for which p is a prime factor (p positive) of ij] ...] + * P will be sorted by increasing order of the prime numbers. The final result has + * to be given as a string in Java. + * Example: + * I = {12, 15}; // result = "(2 12)(3 27)(5 15)" + */ +public final class SumOfDivided { + + /** + * Private constructor for the Sum of Divided utility class. + */ + private SumOfDivided() { + + } + + /** + * This method calculates and return the string representation of the sum of + * divided primes. + * + * @param numbers the numbers array. + * @return the result in String representation. + */ + public static String sumOfDivided(int[] numbers) { + StringBuilder result = new StringBuilder(); + IntStream.rangeClosed(2, Arrays.stream(numbers).map(Math::abs).sum() / 2).filter(SumOfDivided::isPrime) + .forEach(n -> result.append(sumMultiples(n, numbers))); + return result.toString(); + } + + /** + * This method sum all multiples of a prime number located in the array. + * + * @param prime the prime number. + * @param numbers the array numbers. + * @return the string representation result. + */ + private static String sumMultiples(int prime, int[] numbers) { + List list = Arrays.stream(numbers).filter(n -> n % prime == 0) + .boxed().collect(Collectors.toList()); + return list.size() != 0 + ? String.format("(%d %d)", prime, list.stream().mapToInt(i -> i).sum()) : ""; + } + + /** + * This method verifies if a number is prime. + * + * @param number number to be evaluated. + * @return the verification result. + */ + private static boolean isPrime(int number) { + return IntStream.rangeClosed(2, (int) Math.sqrt(number)).allMatch(n -> number % n != 0); + } +} diff --git a/src/test/java/org/fundacionjala/coding/marcos/SumOfDividedTest.java b/src/test/java/org/fundacionjala/coding/marcos/SumOfDividedTest.java new file mode 100644 index 0000000..bd71c60 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/marcos/SumOfDividedTest.java @@ -0,0 +1,47 @@ +package org.fundacionjala.coding.marcos; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by Marcos. + */ +public class SumOfDividedTest { + + /** + * Basic test using positive numbers. + */ + @Test + public void testOne() { + int[] first = new int[]{12, 15}; + assertEquals("(2 12)(3 27)(5 15)", SumOfDivided.sumOfDivided(first)); + } + + /** + * Basic test using positive and negative numbers. + */ + @Test + public void testTwo() { + int[] second = new int[]{15, 30, -45}; + assertEquals("(2 30)(3 0)(5 0)", SumOfDivided.sumOfDivided(second)); + } + + /** + * Basic test using large positive numbers. + */ + @Test + public void testThree() { + int[] third = new int[]{46, 52, 100, 64}; + assertEquals("(2 262)(5 100)(13 52)(23 46)", SumOfDivided.sumOfDivided(third)); + } + + /** + * Basic test using negative numbers. + */ + @Test + public void testFour() { + int[] fourth = new int[]{-35, -24, -48, -11}; + assertEquals("(2 -72)(3 -72)(5 -35)(7 -35)(11 -11)", SumOfDivided.sumOfDivided(fourth)); + } +} From 4fbe7ec12bf3ec60b506f9c5fa1c82fd193cef84 Mon Sep 17 00:00:00 2001 From: marcos-x86 Date: Mon, 4 Sep 2017 02:09:14 -0400 Subject: [PATCH 2/3] Constants added and Test Coverage incremented --- .../coding/marcos/SumOfDivided.java | 9 +++++--- .../coding/marcos/SumOfDividedTest.java | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/fundacionjala/coding/marcos/SumOfDivided.java b/src/main/java/org/fundacionjala/coding/marcos/SumOfDivided.java index 30c3f0b..18eceaf 100644 --- a/src/main/java/org/fundacionjala/coding/marcos/SumOfDivided.java +++ b/src/main/java/org/fundacionjala/coding/marcos/SumOfDivided.java @@ -18,6 +18,9 @@ */ public final class SumOfDivided { + public static final int TWO = 2; + public static final int ZERO = 0; + /** * Private constructor for the Sum of Divided utility class. */ @@ -34,7 +37,7 @@ private SumOfDivided() { */ public static String sumOfDivided(int[] numbers) { StringBuilder result = new StringBuilder(); - IntStream.rangeClosed(2, Arrays.stream(numbers).map(Math::abs).sum() / 2).filter(SumOfDivided::isPrime) + IntStream.rangeClosed(TWO, Arrays.stream(numbers).map(Math::abs).sum() / TWO).filter(SumOfDivided::isPrime) .forEach(n -> result.append(sumMultiples(n, numbers))); return result.toString(); } @@ -49,7 +52,7 @@ public static String sumOfDivided(int[] numbers) { private static String sumMultiples(int prime, int[] numbers) { List list = Arrays.stream(numbers).filter(n -> n % prime == 0) .boxed().collect(Collectors.toList()); - return list.size() != 0 + return list.size() != ZERO ? String.format("(%d %d)", prime, list.stream().mapToInt(i -> i).sum()) : ""; } @@ -60,6 +63,6 @@ private static String sumMultiples(int prime, int[] numbers) { * @return the verification result. */ private static boolean isPrime(int number) { - return IntStream.rangeClosed(2, (int) Math.sqrt(number)).allMatch(n -> number % n != 0); + return IntStream.rangeClosed(TWO, (int) Math.sqrt(number)).allMatch(n -> number % n != ZERO); } } diff --git a/src/test/java/org/fundacionjala/coding/marcos/SumOfDividedTest.java b/src/test/java/org/fundacionjala/coding/marcos/SumOfDividedTest.java index bd71c60..f06c80f 100644 --- a/src/test/java/org/fundacionjala/coding/marcos/SumOfDividedTest.java +++ b/src/test/java/org/fundacionjala/coding/marcos/SumOfDividedTest.java @@ -1,14 +1,36 @@ package org.fundacionjala.coding.marcos; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; + import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * Created by Marcos. */ public class SumOfDividedTest { + /** + * This test assert the modifier of the Sequence private constructor class. + * + * @throws NoSuchMethodException throw when no private constructor is defined. + * @throws IllegalAccessException throw when can not access to the constructor. + * @throws InvocationTargetException throw when can not be perform a invocation. + * @throws InstantiationException throw when a instantiation can not be performed. + */ + @Test + public void privateConstructorTest() throws NoSuchMethodException, IllegalAccessException, + InvocationTargetException, InstantiationException { + Constructor constructor = SumOfDivided.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } + /** * Basic test using positive numbers. */ From b8704044135629125f85d0e1d90ee2552ac2ebf9 Mon Sep 17 00:00:00 2001 From: marcos-x86 Date: Mon, 4 Sep 2017 02:24:32 -0400 Subject: [PATCH 3/3] Test with negative and positive numbers added --- .../fundacionjala/coding/marcos/SumOfDividedTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/java/org/fundacionjala/coding/marcos/SumOfDividedTest.java b/src/test/java/org/fundacionjala/coding/marcos/SumOfDividedTest.java index f06c80f..ad5d3d4 100644 --- a/src/test/java/org/fundacionjala/coding/marcos/SumOfDividedTest.java +++ b/src/test/java/org/fundacionjala/coding/marcos/SumOfDividedTest.java @@ -66,4 +66,13 @@ public void testFour() { int[] fourth = new int[]{-35, -24, -48, -11}; assertEquals("(2 -72)(3 -72)(5 -35)(7 -35)(11 -11)", SumOfDivided.sumOfDivided(fourth)); } + + /** + * Basic test using negative and positive numbers. + */ + @Test + public void testFive() { + int[] fourth = new int[]{10, 55, -48, -21}; + assertEquals("(2 -38)(3 -69)(5 65)(7 -21)(11 55)", SumOfDivided.sumOfDivided(fourth)); + } }