Skip to content

largest-series-product: add to track #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 14, 2016
Merged
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
8 changes: 7 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"nth-prime",
"pascals-triangle",
"beer-song",
"difference-of-squares"
"difference-of-squares",
"largest-series-product"
],
"exercises": [
{
Expand Down Expand Up @@ -245,6 +246,11 @@
"slug": "difference-of-squares",
"difficulty": 1,
"topics": []
},
{
"slug": "largest-series-product",
"difficulty": 1,
"topics": []
}
],
"deprecated": [
Expand Down
17 changes: 17 additions & 0 deletions exercises/largest-series-product/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"

repositories {
mavenCentral()
}

dependencies {
testCompile "junit:junit:4.12"
}
test {
testLogging {
exceptionFormat = 'full'
events = ["passed", "failed", "skipped"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
public final class LargestSeriesProductCalculator {

private final String stringToSearch;

public LargestSeriesProductCalculator(final String stringToSearch) throws IllegalArgumentException {
this.stringToSearch = stringToSearch;
validateStringToSearch();
}

public long calculateLargestProductForSeriesLength(final int seriesLength) throws IllegalArgumentException {
if (seriesLength < 0) {
throw new IllegalArgumentException("Series length must be non-negative.");
} else if (seriesLength == 0) {
return 1;
} else if (seriesLength > stringToSearch.length()) {
throw new IllegalArgumentException(
"Series length must be less than or equal to the length of the string to search.");
} else {
long result = 0;

int numberOfSeriesToCheck = stringToSearch.length() - seriesLength + 1;

for (int startIndex = 0; startIndex < numberOfSeriesToCheck; startIndex++) {
/*
* Note: computing the product of each series fresh each time is not the most efficient solution, but
* it's the simplest to reason about.
*/
result = Math.max(result, computeProductOfSeries(startIndex, seriesLength));
}

return result;
}
}

private void validateStringToSearch() throws IllegalArgumentException {
if (stringToSearch == null) {
throw new IllegalArgumentException("String to search must be non-null.");
} else if (!stringToSearch.chars().allMatch(Character::isDigit)) {
throw new IllegalArgumentException("String to search may only contains digits.");
}
}

private long computeProductOfSeries(final int startIndex, final int seriesLength) {
// The multiplicative identity is 1.
long result = 1;

final int endIndex = startIndex + seriesLength - 1;

for (int characterIndex = startIndex; characterIndex <= endIndex; characterIndex++) {
result = result * Character.getNumericValue(stringToSearch.charAt(characterIndex));
}

return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class LargestSeriesProductCalculator {



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;

public final class LargestSeriesProductCalculatorTest {

/*
* See https://github.com/junit-team/junit4/wiki/Rules for information on JUnit Rules in general and
* ExpectedExceptions in particular.
*/
@Rule
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once discussion in #171 is complete, I will update with an appropriate comment to introduce the user to JUnit Rules here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way you did it there was right on.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll hold off on merging until you signal you're done with this part.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment has now been added!

public ExpectedException expectedException = ExpectedException.none();

@Test
public void testCorrectlyCalculatesLargestProductOfLengthTwoWithNumbersInOrder() {
final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("0123456789");
final long expectedProduct = 72;

final long actualProduct = calculator.calculateLargestProductForSeriesLength(2);

assertEquals(expectedProduct, actualProduct);
}

@Ignore
@Test
public void testCorrectlyCalculatesLargestProductOfLengthTwoWithNumbersNotInOrder() {
final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("576802143");
final long expectedProduct = 48;

final long actualProduct = calculator.calculateLargestProductForSeriesLength(2);

assertEquals(expectedProduct, actualProduct);
}

@Ignore
@Test
public void testCorrectlyCalculatesLargestProductWhenSeriesLengthEqualsStringToSearchLength() {
final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("29");
final long expectedProduct = 18;

final long actualProduct = calculator.calculateLargestProductForSeriesLength(2);

assertEquals(expectedProduct, actualProduct);
}

@Ignore
@Test
public void testCorrectlyCalculatesLargestProductOfLengthThreeWithNumbersInOrder() {
final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("0123456789");
final long expectedProduct = 504;

final long actualProduct = calculator.calculateLargestProductForSeriesLength(3);

assertEquals(expectedProduct, actualProduct);
}

@Ignore
@Test
public void testCorrectlyCalculatesLargestProductOfLengthThreeWithNumbersNotInOrder() {
final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("1027839564");
final long expectedProduct = 270;

final long actualProduct = calculator.calculateLargestProductForSeriesLength(3);

assertEquals(expectedProduct, actualProduct);
}

@Ignore
@Test
public void testCorrectlyCalculatesLargestProductOfLengthFiveWithNumbersInOrder() {
final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("0123456789");
final long expectedProduct = 15120;

final long actualProduct = calculator.calculateLargestProductForSeriesLength(5);

assertEquals(expectedProduct, actualProduct);
}

@Ignore
@Test
public void testCorrectlyCalculatesLargestProductInLongStringToSearchV1() {
final LargestSeriesProductCalculator calculator
= new LargestSeriesProductCalculator("73167176531330624919225119674426574742355349194934");

final long expectedProduct = 23520;

final long actualProduct = calculator.calculateLargestProductForSeriesLength(6);

assertEquals(expectedProduct, actualProduct);
}

@Ignore
@Test
public void testCorrectlyCalculatesLargestProductInLongStringToSearchV2() {
final LargestSeriesProductCalculator calculator
= new LargestSeriesProductCalculator("52677741234314237566414902593461595376319419139427");

final long expectedProduct = 28350;

final long actualProduct = calculator.calculateLargestProductForSeriesLength(6);

assertEquals(expectedProduct, actualProduct);
}

@Ignore
@Test
public void testCorrectlyCalculatesLargestProductInLongStringToSearchFromProjectEuler() {
final LargestSeriesProductCalculator calculator
= new LargestSeriesProductCalculator("7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450");

final long expectedProduct = 23514624000L;

final long actualProduct = calculator.calculateLargestProductForSeriesLength(13);

assertEquals(expectedProduct, actualProduct);
}

@Ignore
@Test
public void testCorrectlyCalculatesLargestProductOfZeroIfAllDigitsAreZeroes() {
final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("0000");
final long expectedProduct = 0;

final long actualProduct = calculator.calculateLargestProductForSeriesLength(2);

assertEquals(expectedProduct, actualProduct);
}

@Ignore
@Test
public void testCorrectlyCalculatesLargestProductOfZeroIfAllSeriesOfGivenLengthContainZero() {
final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("99099");
final long expectedProduct = 0;

final long actualProduct = calculator.calculateLargestProductForSeriesLength(3);

assertEquals(expectedProduct, actualProduct);
}

@Ignore
@Test
public void testSeriesLengthLongerThanLengthOfStringToTestIsRejected() {
final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("123");

expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(
"Series length must be less than or equal to the length of the string to search.");

calculator.calculateLargestProductForSeriesLength(4);
}

@Ignore
@Test
public void testCorrectlyCalculatesLargestProductOfLength0ForEmptyStringToSearch() {
final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("");
final long expectedProduct = 1;

final long actualProduct = calculator.calculateLargestProductForSeriesLength(0);

assertEquals(expectedProduct, actualProduct);
}

@Ignore
@Test
public void testCorrectlyCalculatesLargestProductOfLength0ForNonEmptyStringToSearch() {
final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("123");
final long expectedProduct = 1;

final long actualProduct = calculator.calculateLargestProductForSeriesLength(0);

assertEquals(expectedProduct, actualProduct);
}

@Ignore
@Test
public void testEmptyStringToSearchAndSeriesOfNonZeroLengthIsRejected() {
final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("");

expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(
"Series length must be less than or equal to the length of the string to search.");

calculator.calculateLargestProductForSeriesLength(1);
}

@Ignore
@Test
public void testStringToSearchContainingNonDigitCharacterIsRejected() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("String to search may only contains digits.");

new LargestSeriesProductCalculator("1234a5");
}

@Ignore
@Test
public void testNegativeSeriesLengthIsRejected() {
final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("12345");

expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Series length must be non-negative.");

calculator.calculateLargestProductForSeriesLength(-1);
}

@Ignore
@Test
public void testNullStringToSearchIsRejected() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("String to search must be non-null.");

new LargestSeriesProductCalculator(null);
}

}
1 change: 1 addition & 0 deletions exercises/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ include 'grade-school'
include 'hamming'
include 'hexadecimal'
include 'hello-world'
include 'largest-series-product'
include 'linked-list'
include 'luhn'
include 'meetup'
Expand Down