-
-
Notifications
You must be signed in to change notification settings - Fork 708
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
exercises/largest-series-product/src/example/java/LargestSeriesProductCalculator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
exercises/largest-series-product/src/main/java/LargestSeriesProductCalculator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class LargestSeriesProductCalculator { | ||
|
||
|
||
|
||
} |
218 changes: 218 additions & 0 deletions
218
exercises/largest-series-product/src/test/java/LargestSeriesProductCalculatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
Rule
s here.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!