Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/main/java/org/fundacionjala/coding/marcos/SumOfDivided.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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.
* <p>
* 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 {

public static final int TWO = 2;
public static final int ZERO = 0;

/**
* 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(TWO, Arrays.stream(numbers).map(Math::abs).sum() / TWO).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<Integer> list = Arrays.stream(numbers).filter(n -> n % prime == 0)
.boxed().collect(Collectors.toList());
return list.size() != ZERO
? 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(TWO, (int) Math.sqrt(number)).allMatch(n -> number % n != ZERO);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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<SumOfDivided> constructor = SumOfDivided.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
constructor.setAccessible(true);
constructor.newInstance();
}

/**
* 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));
}

/**
* 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));
}
}