Skip to content

Commit

Permalink
Merge pull request #4 from jaebradley/between-two-sets
Browse files Browse the repository at this point in the history
Between two sets
  • Loading branch information
jaebradley committed Jul 19, 2017
2 parents 3836a42 + 5b6f8a8 commit a36efe1
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 6 deletions.
38 changes: 38 additions & 0 deletions src/main/java/algorithms/implementations/BetweenTwoSetsImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package algorithms.implementations;

import algorithms.interfaces.BetweenTwoSets;
import algorithms.interfaces.GreatestCommonDivisorCalculator;
import algorithms.interfaces.LeastCommonMultipleCalculator;

import java.util.Set;

public class BetweenTwoSetsImpl implements BetweenTwoSets {
private final GreatestCommonDivisorCalculator gcdCalculator;
private final LeastCommonMultipleCalculator lcmCalculator;

public BetweenTwoSetsImpl(final GreatestCommonDivisorCalculator gcdCalculator, final LeastCommonMultipleCalculator lcmCalculator) {
this.gcdCalculator = gcdCalculator;
this.lcmCalculator = lcmCalculator;
}

@Override
public int betweenValuesCount(final Set<Long> firstValues, final Set<Long> secondValues) {
if (firstValues.size() < 2 || secondValues.size() < 2) {
return 0;
}

final long gcd = this.gcdCalculator.calculateGreatestCommonDivisor(secondValues);
final long lcm = this.lcmCalculator.calculateLeastCommonMultiple(firstValues);

int count = 0;
long lcmMultiple = lcm;
while (lcmMultiple <= gcd) {
if (gcd % lcmMultiple == 0) {
count++;
}
lcmMultiple += lcm;
}

return count;
}
}
7 changes: 7 additions & 0 deletions src/main/java/algorithms/interfaces/BetweenTwoSets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package algorithms.interfaces;

import java.util.Set;

public interface BetweenTwoSets {
int betweenValuesCount(final Set<Long> firstValues, final Set<Long> secondValues);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package algorithms.interfaces;

import java.util.Collection;

public interface GreatestCommonDivisorCalculator {
long calculateGreatestCommonDivisor(final long a, final long b);
long calculateGreatestCommonDivisor(final Collection<Long> values);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package algorithms.interfaces;

import java.util.Collection;

public interface LeastCommonMultipleCalculator {
long calculateLeastCommonMultiple(final long a, final long b);
long calculateLeastCommonMultiple(final Collection<Long> values);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package algorithms.utilities;

import algorithms.interfaces.GreatestCommonDivisorCalculator;

import java.util.Collection;

public class GreatestCommonDivisorCalculatorImpl implements GreatestCommonDivisorCalculator {
@Override
public long calculateGreatestCommonDivisor(final long a, final long b) {
if (a == 0 && b != 0) {
return b;
}

if (b == 0 && a != 0) {
return a;
}

long first = a;
long second = b;

while (first != second) {
if (first > second) {
first = first - second;
} else {
second = second - first;
}
}

return first;
}

@Override
public long calculateGreatestCommonDivisor(final Collection<Long> values) {
if (values.size() < 2) {
throw new RuntimeException("No values");
}

return values.stream()
.reduce(this::calculateGreatestCommonDivisor)
.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package algorithms.utilities;

import algorithms.interfaces.GreatestCommonDivisorCalculator;
import algorithms.interfaces.LeastCommonMultipleCalculator;

import java.util.Collection;

public class LeastCommonMultipleCalculatorImpl implements LeastCommonMultipleCalculator {
private final GreatestCommonDivisorCalculator greatestCommonDivisorCalculator;

public LeastCommonMultipleCalculatorImpl(final GreatestCommonDivisorCalculator greatestCommonDivisorCalculator) {
this.greatestCommonDivisorCalculator = greatestCommonDivisorCalculator;
}

@Override
public long calculateLeastCommonMultiple(final long a, final long b) {
return ( a * b ) / this.greatestCommonDivisorCalculator.calculateGreatestCommonDivisor(a, b);
}

@Override
public long calculateLeastCommonMultiple(final Collection<Long> values) {
if (values.size() < 2) {
throw new RuntimeException("No values");
}

return values.stream()
.reduce(this::calculateLeastCommonMultiple)
.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import algorithms.interfaces.AlphabeticalCharacterWeightCalculator;
import org.junit.Test;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

public class AlphabeticalCharacterWeightCalculatorImplTest {
AlphabeticalCharacterWeightCalculator calculator = new AlphabeticalCharacterWeightCalculatorImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

public class GreatestConsecutiveCharactersCounterImplTest {
final GreatestConsecutiveCharactersCounter counter = new GreatestConsecutiveCharactersCounterImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import algorithms.interfaces.InsertionSorter;
import org.junit.Test;

import static org.junit.Assert.*;
import static org.junit.Assert.assertArrayEquals;

public class InsertionSorterImplTest {
final InsertionSorter sort = new InsertionSorterImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import algorithms.interfaces.SosMutationCounter;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

public class SosMutationCounterImplTest {
final SosMutationCounter counter = new SosMutationCounterImpl();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package algorithms.utilities;

import algorithms.interfaces.GreatestCommonDivisorCalculator;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class GreatestCommonDivisorCalculatorImplTest {
final GreatestCommonDivisorCalculator calculator = new GreatestCommonDivisorCalculatorImpl();

@Test
public void itShouldReturnOneForRelativelyPrimeNumbers() {
final long gcd = calculator.calculateGreatestCommonDivisor(2, 3);
assertEquals(gcd, 1);
}

@Test
public void itShouldReturnNumberForTwoIdenticalValues() {
final long input = 3;
final long gcd = calculator.calculateGreatestCommonDivisor(input, input);
assertEquals(gcd, input);
}

@Test
public void itShouldReturnGCD() {
final long gcd = calculator.calculateGreatestCommonDivisor(1071, 462);
assertEquals(gcd, 21);
}

@Test
public void itShouldReturnNonZeroValueWhenOneInputIsZero() {
final long input = 1;
long gcd = calculator.calculateGreatestCommonDivisor(0, input);
assertEquals(gcd, input);

gcd = calculator.calculateGreatestCommonDivisor(input, 0);
assertEquals(gcd, input);
}

@Test
public void itShouldThrowWhenThereAreNotTwoValues() {
try {
calculator.calculateGreatestCommonDivisor(new ArrayList<>());
} catch (RuntimeException e) {
// expected
}
}

@Test
public void itShouldReturnGCDForMultipleValues() {
final long a = 3;
final long b = 6;
final long c = 9;
final List<Long> values = new ArrayList<>();
values.add(a);
values.add(b);
values.add(c);
assertEquals(calculator.calculateGreatestCommonDivisor(values), a);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package algorithms.utilities;

import algorithms.interfaces.GreatestCommonDivisorCalculator;
import algorithms.interfaces.LeastCommonMultipleCalculator;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class LeastCommonMultipleCalculatorImplTest {
final long gcdConstant = 1;
final GreatestCommonDivisorCalculator constantGcdCalculator = new GreatestCommonDivisorCalculator() {
@Override
public long calculateGreatestCommonDivisor(final long a, final long b) {
return gcdConstant;
}

@Override
public long calculateGreatestCommonDivisor(final Collection<Long> values) {
return 0;
}
};

final LeastCommonMultipleCalculator calculator = new LeastCommonMultipleCalculatorImpl(constantGcdCalculator);

@Test
public void itShouldCalculateLCM() {
final long a = 3;
final long b = 3;
final long expected = ( a * b ) / gcdConstant;
assertEquals(calculator.calculateLeastCommonMultiple(a, b), expected);
}

@Test
public void itShouldThrowWhenValuesAreEmpty() {
try {
calculator.calculateLeastCommonMultiple(new ArrayList<>());
} catch (RuntimeException e) {
// expected
}
}

@Test
public void itShouldReturnLCMValues() {
final long a = 3;
final long b = 6;
final long c = 9;
final List<Long> values = new ArrayList<>();
values.add(a);
values.add(b);
values.add(c);
final long expected = a * b * c;
assertEquals(calculator.calculateLeastCommonMultiple(values), expected);
}
}

0 comments on commit a36efe1

Please sign in to comment.