-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added LargestFiveDigitNumber impl and TCs
- Loading branch information
1 parent
d61babc
commit bd40fdd
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/test/java/io/github/sandeeplakka/codewars/kyu7/LargestFiveDigitNumber.java
This file contains 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,46 @@ | ||
package io.github.sandeeplakka.codewars.kyu7; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/* | ||
Largest 5 digit number in a series | ||
In the following 6 digit number: 283910 | ||
91 is the greatest sequence of 2 consecutive digits. | ||
In the following 10 digit number: 1234567890 | ||
67890 is the greatest sequence of 5 consecutive digits. | ||
Complete the solution so that it returns the greatest sequence of | ||
five consecutive digits found within the number given. | ||
The number will be passed in as a string of only digits. | ||
It should return a five digit integer. The number passed may be as large as 1000 digits. | ||
Adapted from ProjectEuler.net | ||
Categories : Algorithms | ||
*/ | ||
public class LargestFiveDigitNumber { | ||
@Test | ||
public void exampleTests() { | ||
doTest("283910", 83910); | ||
doTest("1234567890", 67890); | ||
doTest("731674765", 74765); | ||
} | ||
|
||
private void doTest(String input, int expected) { | ||
assertEquals(expected, solve(input)); | ||
} | ||
|
||
public static int solve(final String digits) { | ||
int startIdx = 0; | ||
int val = 0; | ||
for (int i = 0; i <= digits.length() - 5; i++) { | ||
int thisVal = Integer.parseInt(digits.substring(startIdx + i, startIdx + i + 5)); | ||
val = Math.max(val, thisVal); | ||
} | ||
return val; | ||
} | ||
} |