Skip to content

Commit 8ff352f

Browse files
committed
Fixed suggested.
Test ordering fixed. Example uses StringBuilders Example uses one loop encode method added braces to one-line if statements etc.
1 parent e7070b0 commit 8ff352f

4 files changed

Lines changed: 183 additions & 149 deletions

File tree

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +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-
}
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: 64 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,65 @@
1-
import java.util.regex.Matcher;
2-
import java.util.regex.Pattern;
3-
4-
public class RunLengthEncoding {
5-
public String encode(String data) {
6-
String encodedData = "";
7-
8-
for (int i = 0; i < data.length(); i++) {
9-
char c = data.charAt(i);
10-
11-
if (i != 0 && c == data.charAt(i - 1)) continue;
12-
13-
int amt = 1;
14-
15-
for (int j = i + 1; j < data.length() && c == data.charAt(j); j++) {
16-
amt++;
17-
}
18-
19-
if (amt == 1) encodedData += c;
20-
else encodedData += amt + Character.toString(c);
21-
}
22-
23-
return encodedData;
24-
}
25-
26-
public String decode(String encodedData) {
27-
String decodedData = "";
28-
Pattern pattern = Pattern.compile("[0-9]+|[a-zA-Z\\s]");
29-
Matcher matcher = pattern.matcher(encodedData);
30-
31-
while (matcher.find()) {
32-
try {
33-
int number = Integer.parseInt(matcher.group());
34-
35-
matcher.find();
36-
37-
while (number-- != 0) {
38-
decodedData += matcher.group();
39-
}
40-
} catch (NumberFormatException e) {
41-
decodedData += matcher.group();
42-
}
43-
}
44-
45-
return decodedData;
46-
}
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 encodedData = new StringBuilder();
8+
9+
if (data.length() == 0) {
10+
return data;
11+
}
12+
13+
int charCount = 0;
14+
char beforeChar = data.charAt(0);
15+
16+
for (int i = 0; i < data.length(); i++) {
17+
char currentChar = data.charAt(i);
18+
19+
if (beforeChar == currentChar) {
20+
charCount++;
21+
22+
if (i != data.length() - 1) {
23+
continue;
24+
}
25+
}
26+
27+
if (charCount != 1) {
28+
encodedData.append(charCount);
29+
}
30+
31+
encodedData.append(beforeChar);
32+
33+
if (beforeChar != currentChar && i == data.length() - 1) {
34+
encodedData.append(currentChar);
35+
}
36+
37+
beforeChar = currentChar;
38+
charCount = 1;
39+
}
40+
41+
return encodedData.toString();
42+
}
43+
44+
public String decode(String encodedData) {
45+
StringBuilder decodedData = new StringBuilder();
46+
Pattern pattern = Pattern.compile("[0-9]+|[a-zA-Z\\s]");
47+
Matcher matcher = pattern.matcher(encodedData);
48+
49+
while (matcher.find()) {
50+
try {
51+
int number = Integer.parseInt(matcher.group());
52+
53+
matcher.find();
54+
55+
for (; number != 0; --number) {
56+
decodedData.append(matcher.group());
57+
}
58+
} catch (NumberFormatException e) {
59+
decodedData.append(matcher.group());
60+
}
61+
}
62+
63+
return decodedData.toString();
64+
}
4765
}
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
public class RunLengthEncoding {
2-
public String encode(String input) {
3-
throw new UnsupportedOperationException("Method has not been implemented yet.");
4-
}
5-
6-
public String decode(String input) {
7-
throw new UnsupportedOperationException("Method has not been implemented yet.");
8-
}
1+
import java.util.regex.Matcher;
2+
import java.util.regex.Pattern;
3+
4+
public class RunLengthEncoding {
5+
public String encode(String data) {
6+
throw new UnsupportedOperationException("Method has not been implemented yet.");
7+
}
8+
9+
public String decode(String encodedData) {
10+
throw new UnsupportedOperationException("Method has not been implemented yet.");
11+
}
912
}
Lines changed: 91 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,92 @@
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-
@Test
20-
public void decodeEmpty() {
21-
Assert.assertEquals("", runLengthEncoding.decode(""));
22-
}
23-
24-
@Ignore("Remove to run test")
25-
@Test
26-
public void encodeWithOnlySingleValues() {
27-
Assert.assertEquals("XYZ", runLengthEncoding.encode("XYZ"));
28-
}
29-
30-
@Ignore("Remove to run test")
31-
@Test
32-
public void decodeWithOnlySingleValues() {
33-
Assert.assertEquals("XYZ", runLengthEncoding.decode("XYZ"));
34-
}
35-
36-
@Ignore("Remove to run test")
37-
@Test
38-
public void encodeWithMixedValues() {
39-
Assert.assertEquals("12WB12W3B24WB", runLengthEncoding.encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"));
40-
}
41-
42-
@Ignore("Remove to run test")
43-
@Test
44-
public void decodeWithMixedValues() {
45-
Assert.assertEquals("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB", runLengthEncoding.decode("12WB12W3B24WB"));
46-
}
47-
48-
@Ignore("Remove to run test")
49-
@Test
50-
public void encodeWithWhitespaceValues() {
51-
Assert.assertEquals("2 hs2q q2w2 ", runLengthEncoding.encode(" hsqq qww "));
52-
}
53-
54-
@Ignore("Remove to run test")
55-
@Test
56-
public void decodeWithWhitespaceValues() {
57-
Assert.assertEquals(" hsqq qww ", runLengthEncoding.decode("2 hs2q q2w2 "));
58-
}
59-
60-
@Ignore("Remove to run test")
61-
@Test
62-
public void encodeWithLowercaseValues() {
63-
Assert.assertEquals("2a3b4c", runLengthEncoding.encode("aabbbcccc"));
64-
}
65-
66-
@Ignore("Remove to run test")
67-
@Test
68-
public void decodeWithLowercaseValues() {
69-
Assert.assertEquals("aabbbcccc", runLengthEncoding.decode("2a3b4c"));
70-
}
71-
72-
@Ignore("Remove to run test")
73-
@Test
74-
public void encodeThenDecode() {
75-
String inOut = "zzz ZZ zZ";
76-
String encoded = runLengthEncoding.encode(inOut);
77-
Assert.assertEquals(inOut, runLengthEncoding.decode(encoded));
78-
}
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("2A3B4C", runLengthEncoding.encode("AABBBCCCC"));
29+
}
30+
31+
@Ignore("Remove to run test")
32+
@Test
33+
public void encodeWithMixedValues() {
34+
Assert.assertEquals("12WB12W3B24WB", runLengthEncoding.encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"));
35+
}
36+
37+
@Ignore("Remove to run test")
38+
@Test
39+
public void encodeWithWhitespaceValues() {
40+
Assert.assertEquals("2 hs2q q2w2 ", runLengthEncoding.encode(" hsqq qww "));
41+
}
42+
43+
@Ignore("Remove to run test")
44+
@Test
45+
public void encodeWithLowercaseValues() {
46+
Assert.assertEquals("2a3b4c", runLengthEncoding.encode("aabbbcccc"));
47+
}
48+
49+
@Ignore("Remove to run test")
50+
@Test
51+
public void decodeEmpty() {
52+
Assert.assertEquals("", runLengthEncoding.decode(""));
53+
}
54+
55+
@Ignore("Remove to run test")
56+
@Test
57+
public void decodeWithOnlySingleValues() {
58+
Assert.assertEquals("XYZ", runLengthEncoding.decode("XYZ"));
59+
}
60+
61+
@Ignore("Remove to run test")
62+
@Test
63+
public void decodeWithNoSingleValues() {
64+
Assert.assertEquals("AABBBCCCC", runLengthEncoding.decode("2A3B4C"));
65+
}
66+
67+
@Ignore("Remove to run test")
68+
@Test
69+
public void decodeWithMixedValues() {
70+
Assert.assertEquals("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB", runLengthEncoding.decode("12WB12W3B24WB"));
71+
}
72+
73+
@Ignore("Remove to run test")
74+
@Test
75+
public void decodeWithWhitespaceValues() {
76+
Assert.assertEquals(" hsqq qww ", runLengthEncoding.decode("2 hs2q q2w2 "));
77+
}
78+
79+
@Ignore("Remove to run test")
80+
@Test
81+
public void decodeWithLowercaseValues() {
82+
Assert.assertEquals("aabbbcccc", runLengthEncoding.decode("2a3b4c"));
83+
}
84+
85+
@Ignore("Remove to run test")
86+
@Test
87+
public void encodeThenDecode() {
88+
String inOut = "zzz ZZ zZ";
89+
String encoded = runLengthEncoding.encode(inOut);
90+
Assert.assertEquals(inOut, runLengthEncoding.decode(encoded));
91+
}
7992
}

0 commit comments

Comments
 (0)