Przeglądaj źródła

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 lat temu
rodzic
commit
d59b8c0ce9

+ 22
- 20
exercises/run-length-encoding/src/example/java/RunLengthEncoding.java Wyświetl plik

@@ -4,41 +4,43 @@ import java.util.regex.Pattern;
4 4
 public class RunLengthEncoding {
5 5
     public String encode(String data) {
6 6
 
7
-        StringBuilder encodedData = new StringBuilder();
7
+        StringBuilder encodedDataBuilder = new StringBuilder();
8 8
 
9 9
         if (data.length() == 0) {
10 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 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 46
     public String decode(String encodedData) {
@@ -52,7 +54,7 @@ public class RunLengthEncoding {
52 54
 
53 55
                 matcher.find();
54 56
 
55
-                for (; number != 0; --number) {
57
+                for (int i = 0; i<number; i++) {
56 58
                     decodedData.append(matcher.group());
57 59
                 }
58 60
             } catch (NumberFormatException e) {

+ 28
- 9
exercises/run-length-encoding/src/test/java/RunLengthEncodingTest.java Wyświetl plik

@@ -25,25 +25,34 @@ public class RunLengthEncodingTest {
25 25
     @Ignore("Remove to run test")
26 26
     @Test
27 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 33
     @Ignore("Remove to run test")
32 34
     @Test
33 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 42
     @Ignore("Remove to run test")
38 43
     @Test
39 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 50
     @Ignore("Remove to run test")
44 51
     @Test
45 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 58
     @Ignore("Remove to run test")
@@ -55,31 +64,41 @@ public class RunLengthEncodingTest {
55 64
     @Ignore("Remove to run test")
56 65
     @Test
57 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 72
     @Ignore("Remove to run test")
62 73
     @Test
63 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 80
     @Ignore("Remove to run test")
68 81
     @Test
69 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 88
     @Ignore("Remove to run test")
74 89
     @Test
75 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 96
     @Ignore("Remove to run test")
80 97
     @Test
81 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 104
     @Ignore("Remove to run test")