Skip to content
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
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@
"slug": "saddle-points",
"difficulty": 1,
"topics": []
},
{
"slug": "run-length-encoding",
"difficulty": 1,
"topics": []
}
],
"deprecated": [
Expand Down
17 changes: 17 additions & 0 deletions exercises/run-length-encoding/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,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();
}
}
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 exercises/run-length-encoding/src/test/java/RunLengthEncodingTest.java
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(""));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There should be an @Ignore here :)

Copy link
Copy Markdown
Member Author

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 @Ignore off of the first/simplest test.

Copy link
Copy Markdown
Contributor

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) :)

@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));
}
}