Skip to content

Commit 7db59e5

Browse files
authored
Merge pull request exercism#522 from Smarticles101/run-length-encoding
run-length-encoding: add to track
2 parents ec3ab21 + b330558 commit 7db59e5

5 files changed

Lines changed: 209 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@
329329
"slug": "saddle-points",
330330
"difficulty": 1,
331331
"topics": []
332+
},
333+
{
334+
"slug": "run-length-encoding",
335+
"difficulty": 1,
336+
"topics": []
332337
}
333338
],
334339
"deprecated": [
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
test {
13+
testLogging {
14+
exceptionFormat = 'full'
15+
events = ["passed", "failed", "skipped"]
16+
}
17+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.util.regex.Matcher;
2+
import java.util.regex.Pattern;
3+
4+
public class RunLengthEncoding {
5+
public String encode(String data) {
6+
7+
StringBuilder encodedDataBuilder = new StringBuilder();
8+
9+
if (data.length() == 0) {
10+
return data;
11+
}
12+
13+
int previousCharCount = 1;
14+
char previousChar = data.charAt(0);
15+
16+
for (int i = 1; i < data.length(); i++) {
17+
char currentChar = data.charAt(i);
18+
19+
if (previousChar == currentChar) {
20+
previousCharCount++;
21+
}
22+
23+
if (previousChar != currentChar || i == data.length() - 1) {
24+
addChars(encodedDataBuilder, previousChar, previousCharCount);
25+
26+
if (previousChar != currentChar && i == data.length() - 1) {
27+
addChars(encodedDataBuilder, currentChar, 1);
28+
}
29+
30+
previousChar = currentChar;
31+
previousCharCount = 1;
32+
}
33+
}
34+
35+
return encodedDataBuilder.toString();
36+
}
37+
38+
public void addChars(StringBuilder toAddTo, char toAdd, int countOfChar) {
39+
if(countOfChar != 1) {
40+
toAddTo.append(countOfChar);
41+
}
42+
43+
toAddTo.append(toAdd);
44+
}
45+
46+
public String decode(String encodedData) {
47+
StringBuilder decodedData = new StringBuilder();
48+
Pattern pattern = Pattern.compile("[0-9]+|[a-zA-Z\\s]");
49+
Matcher matcher = pattern.matcher(encodedData);
50+
51+
while (matcher.find()) {
52+
try {
53+
int number = Integer.parseInt(matcher.group());
54+
55+
matcher.find();
56+
57+
for (int i = 0; i<number; i++) {
58+
decodedData.append(matcher.group());
59+
}
60+
} catch (NumberFormatException e) {
61+
decodedData.append(matcher.group());
62+
}
63+
}
64+
65+
return decodedData.toString();
66+
}
67+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class RunLengthEncoding {
2+
public String encode(String data) {
3+
throw new UnsupportedOperationException("Method has not been implemented yet.");
4+
}
5+
6+
public String decode(String encodedData) {
7+
throw new UnsupportedOperationException("Method has not been implemented yet.");
8+
}
9+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import org.junit.Assert;
2+
import org.junit.Before;
3+
import org.junit.Ignore;
4+
import org.junit.Test;
5+
6+
public class RunLengthEncodingTest {
7+
private RunLengthEncoding runLengthEncoding;
8+
9+
@Before
10+
public void setUp() {
11+
runLengthEncoding = new RunLengthEncoding();
12+
}
13+
14+
@Test
15+
public void encodeEmpty() {
16+
Assert.assertEquals("", runLengthEncoding.encode(""));
17+
}
18+
19+
@Ignore("Remove to run test")
20+
@Test
21+
public void encodeWithOnlySingleValues() {
22+
Assert.assertEquals("XYZ", runLengthEncoding.encode("XYZ"));
23+
}
24+
25+
@Ignore("Remove to run test")
26+
@Test
27+
public void encodeWithNoSingleValues() {
28+
Assert.assertEquals(
29+
"2A3B4C",
30+
runLengthEncoding.encode("AABBBCCCC"));
31+
}
32+
33+
@Ignore("Remove to run test")
34+
@Test
35+
public void encodeWithMixedValues() {
36+
Assert.assertEquals(
37+
"12WB12W3B24WB",
38+
runLengthEncoding.encode(
39+
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"));
40+
}
41+
42+
@Ignore("Remove to run test")
43+
@Test
44+
public void encodeWithWhitespaceValues() {
45+
Assert.assertEquals(
46+
"2 hs2q q2w2 ",
47+
runLengthEncoding.encode(" hsqq qww "));
48+
}
49+
50+
@Ignore("Remove to run test")
51+
@Test
52+
public void encodeWithLowercaseValues() {
53+
Assert.assertEquals(
54+
"2a3b4c",
55+
runLengthEncoding.encode("aabbbcccc"));
56+
}
57+
58+
@Ignore("Remove to run test")
59+
@Test
60+
public void decodeEmpty() {
61+
Assert.assertEquals("", runLengthEncoding.decode(""));
62+
}
63+
64+
@Ignore("Remove to run test")
65+
@Test
66+
public void decodeWithOnlySingleValues() {
67+
Assert.assertEquals(
68+
"XYZ",
69+
runLengthEncoding.decode("XYZ"));
70+
}
71+
72+
@Ignore("Remove to run test")
73+
@Test
74+
public void decodeWithNoSingleValues() {
75+
Assert.assertEquals(
76+
"AABBBCCCC",
77+
runLengthEncoding.decode("2A3B4C"));
78+
}
79+
80+
@Ignore("Remove to run test")
81+
@Test
82+
public void decodeWithMixedValues() {
83+
Assert.assertEquals(
84+
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB",
85+
runLengthEncoding.decode("12WB12W3B24WB"));
86+
}
87+
88+
@Ignore("Remove to run test")
89+
@Test
90+
public void decodeWithWhitespaceValues() {
91+
Assert.assertEquals(
92+
" hsqq qww ",
93+
runLengthEncoding.decode("2 hs2q q2w2 "));
94+
}
95+
96+
@Ignore("Remove to run test")
97+
@Test
98+
public void decodeWithLowercaseValues() {
99+
Assert.assertEquals(
100+
"aabbbcccc",
101+
runLengthEncoding.decode("2a3b4c"));
102+
}
103+
104+
@Ignore("Remove to run test")
105+
@Test
106+
public void encodeThenDecode() {
107+
String inOut = "zzz ZZ zZ";
108+
String encoded = runLengthEncoding.encode(inOut);
109+
Assert.assertEquals(inOut, runLengthEncoding.decode(encoded));
110+
}
111+
}

0 commit comments

Comments
 (0)