Bladeren bron

Example encoding should be simpler and more-readable now.

Made a for loop more typical.
Test assertion parameters are on seperate lines.
Logan Stucki 9 jaren geleden
bovenliggende
commit
d59b8c0ce9

+ 22
- 20
exercises/run-length-encoding/src/example/java/RunLengthEncoding.java Bestand weergeven

4
 public class RunLengthEncoding {
4
 public class RunLengthEncoding {
5
     public String encode(String data) {
5
     public String encode(String data) {
6
 
6
 
7
-        StringBuilder encodedData = new StringBuilder();
7
+        StringBuilder encodedDataBuilder = new StringBuilder();
8
 
8
 
9
         if (data.length() == 0) {
9
         if (data.length() == 0) {
10
             return data;
10
             return data;
11
         }
11
         }
12
 
12
 
13
-        int charCount = 0;
14
-        char beforeChar = data.charAt(0);
13
+        int previousCharCount = 1;
14
+        char previousChar = data.charAt(0);
15
 
15
 
16
-        for (int i = 0; i < data.length(); i++) {
16
+        for (int i = 1; i < data.length(); i++) {
17
             char currentChar = data.charAt(i);
17
             char currentChar = data.charAt(i);
18
 
18
 
19
-            if (beforeChar == currentChar) {
20
-                charCount++;
21
-
22
-                if (i != data.length() - 1) {
23
-                    continue;
24
-                }
19
+            if (previousChar == currentChar) {
20
+                previousCharCount++;
25
             }
21
             }
26
 
22
 
27
-            if (charCount != 1) {
28
-                encodedData.append(charCount);
29
-            }
23
+            if (previousChar != currentChar || i == data.length() - 1) {
24
+                addChars(encodedDataBuilder, previousChar, previousCharCount);
30
 
25
 
31
-            encodedData.append(beforeChar);
26
+                if (previousChar != currentChar && i == data.length() - 1) {
27
+                    addChars(encodedDataBuilder, currentChar, 1);
28
+                }
32
 
29
 
33
-            if (beforeChar != currentChar && i == data.length() - 1) {
34
-                encodedData.append(currentChar);
30
+                previousChar = currentChar;
31
+                previousCharCount = 1;
35
             }
32
             }
33
+        }
34
+
35
+        return encodedDataBuilder.toString();
36
+    }
36
 
37
 
37
-            beforeChar = currentChar;
38
-            charCount = 1;
38
+    public void addChars(StringBuilder toAddTo, char toAdd, int countOfChar) {
39
+        if(countOfChar != 1) {
40
+            toAddTo.append(countOfChar);
39
         }
41
         }
40
 
42
 
41
-        return encodedData.toString();
43
+        countOfChar.append(toAdd);
42
     }
44
     }
43
 
45
 
44
     public String decode(String encodedData) {
46
     public String decode(String encodedData) {
52
 
54
 
53
                 matcher.find();
55
                 matcher.find();
54
 
56
 
55
-                for (; number != 0; --number) {
57
+                for (int i = 0; i<number; i++) {
56
                     decodedData.append(matcher.group());
58
                     decodedData.append(matcher.group());
57
                 }
59
                 }
58
             } catch (NumberFormatException e) {
60
             } catch (NumberFormatException e) {

+ 28
- 9
exercises/run-length-encoding/src/test/java/RunLengthEncodingTest.java Bestand weergeven

25
     @Ignore("Remove to run test")
25
     @Ignore("Remove to run test")
26
     @Test
26
     @Test
27
     public void encodeWithNoSingleValues() {
27
     public void encodeWithNoSingleValues() {
28
-        Assert.assertEquals("2A3B4C", runLengthEncoding.encode("AABBBCCCC"));
28
+        Assert.assertEquals(
29
+                "2A3B4C",
30
+                runLengthEncoding.encode("AABBBCCCC"));
29
     }
31
     }
30
 
32
 
31
     @Ignore("Remove to run test")
33
     @Ignore("Remove to run test")
32
     @Test
34
     @Test
33
     public void encodeWithMixedValues() {
35
     public void encodeWithMixedValues() {
34
-        Assert.assertEquals("12WB12W3B24WB", runLengthEncoding.encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"));
36
+        Assert.assertEquals(
37
+                "12WB12W3B24WB",
38
+                runLengthEncoding.encode(
39
+                        "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"));
35
     }
40
     }
36
 
41
 
37
     @Ignore("Remove to run test")
42
     @Ignore("Remove to run test")
38
     @Test
43
     @Test
39
     public void encodeWithWhitespaceValues() {
44
     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  "));
41
     }
48
     }
42
 
49
 
43
     @Ignore("Remove to run test")
50
     @Ignore("Remove to run test")
44
     @Test
51
     @Test
45
     public void encodeWithLowercaseValues() {
52
     public void encodeWithLowercaseValues() {
46
-        Assert.assertEquals("2a3b4c", runLengthEncoding.encode("aabbbcccc"));
53
+        Assert.assertEquals(
54
+                "2a3b4c",
55
+                runLengthEncoding.encode("aabbbcccc"));
47
     }
56
     }
48
 
57
 
49
     @Ignore("Remove to run test")
58
     @Ignore("Remove to run test")
55
     @Ignore("Remove to run test")
64
     @Ignore("Remove to run test")
56
     @Test
65
     @Test
57
     public void decodeWithOnlySingleValues() {
66
     public void decodeWithOnlySingleValues() {
58
-        Assert.assertEquals("XYZ", runLengthEncoding.decode("XYZ"));
67
+        Assert.assertEquals(
68
+                "XYZ",
69
+                runLengthEncoding.decode("XYZ"));
59
     }
70
     }
60
 
71
 
61
     @Ignore("Remove to run test")
72
     @Ignore("Remove to run test")
62
     @Test
73
     @Test
63
     public void decodeWithNoSingleValues() {
74
     public void decodeWithNoSingleValues() {
64
-        Assert.assertEquals("AABBBCCCC", runLengthEncoding.decode("2A3B4C"));
75
+        Assert.assertEquals(
76
+                "AABBBCCCC",
77
+                runLengthEncoding.decode("2A3B4C"));
65
     }
78
     }
66
 
79
 
67
     @Ignore("Remove to run test")
80
     @Ignore("Remove to run test")
68
     @Test
81
     @Test
69
     public void decodeWithMixedValues() {
82
     public void decodeWithMixedValues() {
70
-        Assert.assertEquals("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB", runLengthEncoding.decode("12WB12W3B24WB"));
83
+        Assert.assertEquals(
84
+                "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB",
85
+                runLengthEncoding.decode("12WB12W3B24WB"));
71
     }
86
     }
72
 
87
 
73
     @Ignore("Remove to run test")
88
     @Ignore("Remove to run test")
74
     @Test
89
     @Test
75
     public void decodeWithWhitespaceValues() {
90
     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 "));
77
     }
94
     }
78
 
95
 
79
     @Ignore("Remove to run test")
96
     @Ignore("Remove to run test")
80
     @Test
97
     @Test
81
     public void decodeWithLowercaseValues() {
98
     public void decodeWithLowercaseValues() {
82
-        Assert.assertEquals("aabbbcccc", runLengthEncoding.decode("2a3b4c"));
99
+        Assert.assertEquals(
100
+                "aabbbcccc",
101
+                runLengthEncoding.decode("2a3b4c"));
83
     }
102
     }
84
 
103
 
85
     @Ignore("Remove to run test")
104
     @Ignore("Remove to run test")