Skip to content

Commit d59b8c0

Browse files
committed
Example encoding should be simpler and more-readable now.
Made a for loop more typical. Test assertion parameters are on seperate lines.
1 parent 052dbef commit d59b8c0

2 files changed

Lines changed: 50 additions & 29 deletions

File tree

‎exercises/run-length-encoding/src/example/java/RunLengthEncoding.java‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,43 @@
44
public class RunLengthEncoding {
55
public String encode(String data) {
66

7-
StringBuilder encodedData = new StringBuilder();
7+
StringBuilder encodedDataBuilder = new StringBuilder();
88

99
if (data.length() == 0) {
1010
return data;
1111
}
1212

13-
int charCount = 0;
14-
char beforeChar = data.charAt(0);
13+
int previousCharCount = 1;
14+
char previousChar = data.charAt(0);
1515

16-
for (int i = 0; i < data.length(); i++) {
16+
for (int i = 1; i < data.length(); i++) {
1717
char currentChar = data.charAt(i);
1818

19-
if (beforeChar == currentChar) {
20-
charCount++;
21-
22-
if (i != data.length() - 1) {
23-
continue;
24-
}
19+
if (previousChar == currentChar) {
20+
previousCharCount++;
2521
}
2622

27-
if (charCount != 1) {
28-
encodedData.append(charCount);
29-
}
23+
if (previousChar != currentChar || i == data.length() - 1) {
24+
addChars(encodedDataBuilder, previousChar, previousCharCount);
3025

31-
encodedData.append(beforeChar);
26+
if (previousChar != currentChar && i == data.length() - 1) {
27+
addChars(encodedDataBuilder, currentChar, 1);
28+
}
3229

33-
if (beforeChar != currentChar && i == data.length() - 1) {
34-
encodedData.append(currentChar);
30+
previousChar = currentChar;
31+
previousCharCount = 1;
3532
}
33+
}
34+
35+
return encodedDataBuilder.toString();
36+
}
3637

37-
beforeChar = currentChar;
38-
charCount = 1;
38+
public void addChars(StringBuilder toAddTo, char toAdd, int countOfChar) {
39+
if(countOfChar != 1) {
40+
toAddTo.append(countOfChar);
3941
}
4042

41-
return encodedData.toString();
43+
countOfChar.append(toAdd);
4244
}
4345

4446
public String decode(String encodedData) {
@@ -52,7 +54,7 @@ public String decode(String encodedData) {
5254

5355
matcher.find();
5456

55-
for (; number != 0; --number) {
57+
for (int i = 0; i<number; i++) {
5658
decodedData.append(matcher.group());
5759
}
5860
} catch (NumberFormatException e) {

‎exercises/run-length-encoding/src/test/java/RunLengthEncodingTest.java‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,34 @@ public void encodeWithOnlySingleValues() {
2525
@Ignore("Remove to run test")
2626
@Test
2727
public void encodeWithNoSingleValues() {
28-
Assert.assertEquals("2A3B4C", runLengthEncoding.encode("AABBBCCCC"));
28+
Assert.assertEquals(
29+
"2A3B4C",
30+
runLengthEncoding.encode("AABBBCCCC"));
2931
}
3032

3133
@Ignore("Remove to run test")
3234
@Test
3335
public void encodeWithMixedValues() {
34-
Assert.assertEquals("12WB12W3B24WB", runLengthEncoding.encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"));
36+
Assert.assertEquals(
37+
"12WB12W3B24WB",
38+
runLengthEncoding.encode(
39+
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"));
3540
}
3641

3742
@Ignore("Remove to run test")
3843
@Test
3944
public void encodeWithWhitespaceValues() {
40-
Assert.assertEquals("2 hs2q q2w2 ", runLengthEncoding.encode(" hsqq qww "));
45+
Assert.assertEquals(
46+
"2 hs2q q2w2 ",
47+
runLengthEncoding.encode(" hsqq qww "));
4148
}
4249

4350
@Ignore("Remove to run test")
4451
@Test
4552
public void encodeWithLowercaseValues() {
46-
Assert.assertEquals("2a3b4c", runLengthEncoding.encode("aabbbcccc"));
53+
Assert.assertEquals(
54+
"2a3b4c",
55+
runLengthEncoding.encode("aabbbcccc"));
4756
}
4857

4958
@Ignore("Remove to run test")
@@ -55,31 +64,41 @@ public void decodeEmpty() {
5564
@Ignore("Remove to run test")
5665
@Test
5766
public void decodeWithOnlySingleValues() {
58-
Assert.assertEquals("XYZ", runLengthEncoding.decode("XYZ"));
67+
Assert.assertEquals(
68+
"XYZ",
69+
runLengthEncoding.decode("XYZ"));
5970
}
6071

6172
@Ignore("Remove to run test")
6273
@Test
6374
public void decodeWithNoSingleValues() {
64-
Assert.assertEquals("AABBBCCCC", runLengthEncoding.decode("2A3B4C"));
75+
Assert.assertEquals(
76+
"AABBBCCCC",
77+
runLengthEncoding.decode("2A3B4C"));
6578
}
6679

6780
@Ignore("Remove to run test")
6881
@Test
6982
public void decodeWithMixedValues() {
70-
Assert.assertEquals("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB", runLengthEncoding.decode("12WB12W3B24WB"));
83+
Assert.assertEquals(
84+
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB",
85+
runLengthEncoding.decode("12WB12W3B24WB"));
7186
}
7287

7388
@Ignore("Remove to run test")
7489
@Test
7590
public void decodeWithWhitespaceValues() {
76-
Assert.assertEquals(" hsqq qww ", runLengthEncoding.decode("2 hs2q q2w2 "));
91+
Assert.assertEquals(
92+
" hsqq qww ",
93+
runLengthEncoding.decode("2 hs2q q2w2 "));
7794
}
7895

7996
@Ignore("Remove to run test")
8097
@Test
8198
public void decodeWithLowercaseValues() {
82-
Assert.assertEquals("aabbbcccc", runLengthEncoding.decode("2a3b4c"));
99+
Assert.assertEquals(
100+
"aabbbcccc",
101+
runLengthEncoding.decode("2a3b4c"));
83102
}
84103

85104
@Ignore("Remove to run test")

0 commit comments

Comments
 (0)