Repository navigation
run-length-encoding: add to track #522
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
FridaTveit
merged 15 commits into
exercism:master
from
Smarticles101:run-length-encoding
May 12, 2017
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c65693b
Merge remote-tracking branch 'refs/remotes/exercism/master'
Smarticles101 5035b40
dibs: I will implement exercise run-length-encoding
Smarticles101 e748109
Added files and updated config.
Smarticles101 97951b5
Update config.json
Smarticles101 40b0b77
Example completed
Smarticles101 3136f67
Added space to regex in example
Smarticles101 8a2ccdb
Fixed illegal escape
Smarticles101 e7070b0
Fixed formatting
Smarticles101 8ff352f
Fixed suggested.
Smarticles101 4ece146
Update RunLengthEncoding.java
Smarticles101 4256139
Update config
Smarticles101 052dbef
Merge branch 'run-length-encoding' of https://github.com/Smarticles10…
Smarticles101 d59b8c0
Example encoding should be simpler and more-readable now.
Smarticles101 9c1b2ea
Normalized EOF's
Smarticles101 b330558
Fixed small error
Smarticles101 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"] | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
exercises/run-length-encoding/src/example/java/RunLengthEncoding.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,67 @@ | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class RunLengthEncoding { | ||
| public String encode(String data) { | ||
|
|
||
| StringBuilder encodedDataBuilder = new StringBuilder(); | ||
|
|
||
| if (data.length() == 0) { | ||
| return data; | ||
| } | ||
|
|
||
| int previousCharCount = 1; | ||
| char previousChar = data.charAt(0); | ||
|
|
||
| for (int i = 1; i < data.length(); i++) { | ||
| char currentChar = data.charAt(i); | ||
|
|
||
| if (previousChar == currentChar) { | ||
| previousCharCount++; | ||
| } | ||
|
|
||
| if (previousChar != currentChar || i == data.length() - 1) { | ||
| addChars(encodedDataBuilder, previousChar, previousCharCount); | ||
|
|
||
| if (previousChar != currentChar && i == data.length() - 1) { | ||
| addChars(encodedDataBuilder, currentChar, 1); | ||
| } | ||
|
|
||
| previousChar = currentChar; | ||
| previousCharCount = 1; | ||
| } | ||
| } | ||
|
|
||
| return encodedDataBuilder.toString(); | ||
| } | ||
|
|
||
| public void addChars(StringBuilder toAddTo, char toAdd, int countOfChar) { | ||
| if(countOfChar != 1) { | ||
| toAddTo.append(countOfChar); | ||
| } | ||
|
|
||
| toAddTo.append(toAdd); | ||
| } | ||
|
|
||
| public String decode(String encodedData) { | ||
| StringBuilder decodedData = new StringBuilder(); | ||
| Pattern pattern = Pattern.compile("[0-9]+|[a-zA-Z\\s]"); | ||
| Matcher matcher = pattern.matcher(encodedData); | ||
|
|
||
| while (matcher.find()) { | ||
| try { | ||
| int number = Integer.parseInt(matcher.group()); | ||
|
|
||
| matcher.find(); | ||
|
|
||
| for (int i = 0; i<number; i++) { | ||
| decodedData.append(matcher.group()); | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| decodedData.append(matcher.group()); | ||
| } | ||
| } | ||
|
|
||
| return decodedData.toString(); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
exercises/run-length-encoding/src/main/java/RunLengthEncoding.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,9 @@ | ||
| public class RunLengthEncoding { | ||
| public String encode(String data) { | ||
| throw new UnsupportedOperationException("Method has not been implemented yet."); | ||
| } | ||
|
|
||
| public String decode(String encodedData) { | ||
| throw new UnsupportedOperationException("Method has not been implemented yet."); | ||
| } | ||
| } |
111 changes: 111 additions & 0 deletions
111
exercises/run-length-encoding/src/test/java/RunLengthEncodingTest.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,111 @@ | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Ignore; | ||
| import org.junit.Test; | ||
|
|
||
| public class RunLengthEncodingTest { | ||
| private RunLengthEncoding runLengthEncoding; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| runLengthEncoding = new RunLengthEncoding(); | ||
| } | ||
|
|
||
| @Test | ||
| public void encodeEmpty() { | ||
| Assert.assertEquals("", runLengthEncoding.encode("")); | ||
| } | ||
|
|
||
| @Ignore("Remove to run test") | ||
| @Test | ||
| public void encodeWithOnlySingleValues() { | ||
| Assert.assertEquals("XYZ", runLengthEncoding.encode("XYZ")); | ||
| } | ||
|
|
||
| @Ignore("Remove to run test") | ||
| @Test | ||
| public void encodeWithNoSingleValues() { | ||
| Assert.assertEquals( | ||
| "2A3B4C", | ||
| runLengthEncoding.encode("AABBBCCCC")); | ||
| } | ||
|
|
||
| @Ignore("Remove to run test") | ||
| @Test | ||
| public void encodeWithMixedValues() { | ||
| Assert.assertEquals( | ||
| "12WB12W3B24WB", | ||
| runLengthEncoding.encode( | ||
| "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB")); | ||
| } | ||
|
|
||
| @Ignore("Remove to run test") | ||
| @Test | ||
| public void encodeWithWhitespaceValues() { | ||
| Assert.assertEquals( | ||
| "2 hs2q q2w2 ", | ||
| runLengthEncoding.encode(" hsqq qww ")); | ||
| } | ||
|
|
||
| @Ignore("Remove to run test") | ||
| @Test | ||
| public void encodeWithLowercaseValues() { | ||
| Assert.assertEquals( | ||
| "2a3b4c", | ||
| runLengthEncoding.encode("aabbbcccc")); | ||
| } | ||
|
|
||
| @Ignore("Remove to run test") | ||
| @Test | ||
| public void decodeEmpty() { | ||
| Assert.assertEquals("", runLengthEncoding.decode("")); | ||
| } | ||
|
|
||
| @Ignore("Remove to run test") | ||
| @Test | ||
| public void decodeWithOnlySingleValues() { | ||
| Assert.assertEquals( | ||
| "XYZ", | ||
| runLengthEncoding.decode("XYZ")); | ||
| } | ||
|
|
||
| @Ignore("Remove to run test") | ||
| @Test | ||
| public void decodeWithNoSingleValues() { | ||
| Assert.assertEquals( | ||
| "AABBBCCCC", | ||
| runLengthEncoding.decode("2A3B4C")); | ||
| } | ||
|
|
||
| @Ignore("Remove to run test") | ||
| @Test | ||
| public void decodeWithMixedValues() { | ||
| Assert.assertEquals( | ||
| "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB", | ||
| runLengthEncoding.decode("12WB12W3B24WB")); | ||
| } | ||
|
|
||
| @Ignore("Remove to run test") | ||
| @Test | ||
| public void decodeWithWhitespaceValues() { | ||
| Assert.assertEquals( | ||
| " hsqq qww ", | ||
| runLengthEncoding.decode("2 hs2q q2w2 ")); | ||
| } | ||
|
|
||
| @Ignore("Remove to run test") | ||
| @Test | ||
| public void decodeWithLowercaseValues() { | ||
| Assert.assertEquals( | ||
| "aabbbcccc", | ||
| runLengthEncoding.decode("2a3b4c")); | ||
| } | ||
|
|
||
| @Ignore("Remove to run test") | ||
| @Test | ||
| public void encodeThenDecode() { | ||
| String inOut = "zzz ZZ zZ"; | ||
| String encoded = runLengthEncoding.encode(inOut); | ||
| Assert.assertEquals(inOut, runLengthEncoding.decode(encoded)); | ||
| } | ||
| } | ||
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.
There should be an
@Ignorehere :)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.
Are you sure? Other tests I have looked at leave the
@Ignoreoff of the first/simplest test.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.
It should only be the first test which doesn't have an
@Ignore. Youv'e left it off the first two (my comment was meant for the second test, sorry if that wasn't clear) :)