Просмотр исходного кода

crypto-square: update to match canonical data (#1178)

* crypto-square: update to match canonical data

* crypto-square: rename crypto to cryptoSquare in tests
FridaTveit 8 лет назад
Родитель
Сommit
d7f1a668bb

+ 0
- 74
exercises/crypto-square/.meta/src/reference/java/Crypto.java Просмотреть файл

@@ -1,74 +0,0 @@
1
-import java.util.ArrayList;
2
-import java.util.List;
3
-
4
-class Crypto {
5
-
6
-    private String normalizedPlaintext;
7
-    private int squareSize;
8
-
9
-    Crypto(String text) {
10
-        this.normalizedPlaintext = normalizeText(text);
11
-        this.squareSize = calculateSquareSize(normalizedPlaintext);
12
-    }
13
-
14
-    String getNormalizedPlaintext() {
15
-        return normalizedPlaintext;
16
-    }
17
-
18
-    int getSquareSize() {
19
-        return squareSize;
20
-    }
21
-
22
-    private static String normalizeText(String text) {
23
-        return text.toLowerCase().codePoints()
24
-                .filter(x -> Character.isLetterOrDigit(x))
25
-                .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
26
-                .toString();
27
-    }
28
-
29
-    private static int calculateSquareSize(String text) {
30
-        return (int) Math.ceil(Math.sqrt(text.length()));
31
-    }
32
-
33
-    List<String> getPlaintextSegments() {
34
-        return getSegmentText(normalizedPlaintext, squareSize);
35
-    }
36
-
37
-    private static List<String> getSegmentText(String text, int squareSize) {
38
-        List<String> segments = new ArrayList<>();
39
-        int index = 0;
40
-
41
-        while (index < text.length()) {
42
-            if (index + squareSize < text.length()) {
43
-                segments.add(text.substring(index, index + squareSize));
44
-            } else {
45
-                segments.add(text.substring(index));
46
-            }
47
-            index += squareSize;
48
-        }
49
-
50
-        return segments;
51
-    }
52
-
53
-    String getCipherText() {
54
-        return getNormalizedCipherText().replaceAll("\\s", "");
55
-    }
56
-
57
-    String getNormalizedCipherText() {
58
-        StringBuilder cipherText = new StringBuilder(normalizedPlaintext.length());
59
-
60
-        for (int index = 0; index < squareSize; index++) {
61
-            for (String segment : getPlaintextSegments()) {
62
-                if (index < segment.length()) {
63
-                    cipherText.append(segment.charAt(index));
64
-                }
65
-            }
66
-
67
-            if (index < squareSize - 1) {
68
-                cipherText.append(" ");
69
-            }
70
-        }
71
-
72
-        return cipherText.toString();
73
-    }
74
-}

+ 62
- 0
exercises/crypto-square/.meta/src/reference/java/CryptoSquare.java Просмотреть файл

@@ -0,0 +1,62 @@
1
+import java.util.ArrayList;
2
+import java.util.List;
3
+
4
+class CryptoSquare {
5
+
6
+    private String ciphertext;
7
+
8
+    CryptoSquare(String plaintext) {
9
+        this.ciphertext = calculateCiphertext(plaintext);
10
+    }
11
+
12
+    String getCiphertext() {
13
+        return ciphertext;
14
+    }
15
+
16
+    private String calculateCiphertext(String plaintext) {
17
+        String normalizedPlaintext = normalizeText(plaintext);
18
+        StringBuilder ciphertext = new StringBuilder(normalizedPlaintext.length());
19
+        int squareSize = calculateSquareSize(normalizedPlaintext);
20
+
21
+        for (int i = 0; i < squareSize; i++) {
22
+            for (String segment : getSegments(normalizedPlaintext, squareSize)) {
23
+                if (i < segment.length()) {
24
+                    ciphertext.append(segment.charAt(i));
25
+                } else {
26
+                    ciphertext.append(" ");
27
+                }
28
+            }
29
+
30
+            if (i < squareSize - 1) {
31
+                ciphertext.append(" ");
32
+            }
33
+        }
34
+
35
+        return ciphertext.toString();
36
+    }
37
+
38
+    private String normalizeText(String plaintext) {
39
+        return plaintext.toLowerCase().codePoints()
40
+                .filter(Character::isLetterOrDigit)
41
+                .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
42
+                .toString();
43
+    }
44
+
45
+    private int calculateSquareSize(String normalizedPlaintext) {
46
+        return (int) Math.ceil(Math.sqrt(normalizedPlaintext.length()));
47
+    }
48
+
49
+    private List<String> getSegments(String normalizedPlaintext, int squareSize) {
50
+        List<String> segments = new ArrayList<>();
51
+
52
+        for (int i = 0; i < normalizedPlaintext.length(); i += squareSize) {
53
+            if (i + squareSize < normalizedPlaintext.length()) {
54
+                segments.add(normalizedPlaintext.substring(i, i + squareSize));
55
+            } else {
56
+                segments.add(normalizedPlaintext.substring(i));
57
+            }
58
+        }
59
+
60
+        return segments;
61
+    }
62
+}

+ 1
- 0
exercises/crypto-square/.meta/version Просмотреть файл

@@ -0,0 +1 @@
1
+3.1.0

+ 28
- 101
exercises/crypto-square/src/test/java/CryptoSquareTest.java Просмотреть файл

@@ -8,138 +8,65 @@ import static org.junit.Assert.assertEquals;
8 8
 
9 9
 public class CryptoSquareTest {
10 10
 
11
-
12
-    @Test
13
-    public void strangeCharactersAreStrippedDuringNormalization() {
14
-        Crypto crypto = new Crypto("s#$%^&plunk");
15
-        String expectedOutput = "splunk";
16
-
17
-        assertEquals(expectedOutput, crypto.getNormalizedPlaintext());
18
-    }
19
-
20
-    @Ignore("Remove to run test")
21
-    @Test
22
-    public void lettersAreLowerCasedDuringNormalization() {
23
-        Crypto crypto = new Crypto("WHOA HEY!");
24
-        String expectedOutput = "whoahey";
25
-
26
-        assertEquals(expectedOutput, crypto.getNormalizedPlaintext());
27
-    }
28
-
29
-    @Ignore("Remove to run test")
30
-    @Test
31
-    public void numbersAreKeptDuringNormalization() {
32
-        Crypto crypto = new Crypto("1, 2, 3, GO!");
33
-        String expectedOutput = "123go";
34
-
35
-        assertEquals(expectedOutput, crypto.getNormalizedPlaintext());
36
-    }
37
-
38
-    @Ignore("Remove to run test")
39
-    @Test
40
-    public void smallestSquareSizeIs2() {
41
-        Crypto crypto = new Crypto("1234");
42
-        int expectedOutput = 2;
43
-
44
-        assertEquals(expectedOutput, crypto.getSquareSize());
45
-    }
46
-
47
-    @Ignore("Remove to run test")
48
-    @Test
49
-    public void sizeOfTextWhoseLengthIsPerfectSquareIsItsSquareRoot() {
50
-        Crypto crypto = new Crypto("123456789");
51
-        int expectedOutput = 3;
52
-
53
-        assertEquals(expectedOutput, crypto.getSquareSize());
54
-    }
55
-
56
-    @Ignore("Remove to run test")
57
-    @Test
58
-    public void sizeOfTextWhoseLengthIsNoPerfectSquareIsNextBiggestSquareRoot() {
59
-        Crypto crypto = new Crypto("123456789abc");
60
-        int expectedOutput = 4;
61
-
62
-        assertEquals(expectedOutput, crypto.getSquareSize());
63
-    }
64
-
65
-    @Ignore("Remove to run test")
66
-    @Test
67
-    public void sizeIsDeterminedByNormalizedText() {
68
-        Crypto crypto = new Crypto("Oh hey, this is nuts!");
69
-        int expectedOutput = 4;
70
-
71
-        assertEquals(expectedOutput, crypto.getSquareSize());
72
-    }
73
-
74
-    @Ignore("Remove to run test")
75
-    @Test
76
-    public void segmentsAreSplitBySquareSize() {
77
-        Crypto crypto = new Crypto("Never vex thine heart with idle woes");
78
-        List<String> expectedOutput = Arrays.asList(new String[]{"neverv", "exthin", "eheart", "withid", "lewoes"});
79
-
80
-        assertEquals(expectedOutput, crypto.getPlaintextSegments());
81
-    }
82
-
83
-    @Ignore("Remove to run test")
84 11
     @Test
85
-    public void segmentsAreSplitBySquareSizeUntilTextRunsOut() {
86
-        Crypto crypto = new Crypto("ZOMG! ZOMBIES!!!");
87
-        List<String> expectedOutput = Arrays.asList(new String[]{"zomg", "zomb", "ies"});
12
+    public void emptyPlaintextResultsInEmptyCiphertext() {
13
+        CryptoSquare cryptoSquare = new CryptoSquare("");
14
+        String expectedOutput = "";
88 15
 
89
-        assertEquals(expectedOutput, crypto.getPlaintextSegments());
16
+        assertEquals(expectedOutput, cryptoSquare.getCiphertext());
90 17
     }
91 18
 
92 19
     @Ignore("Remove to run test")
93 20
     @Test
94
-    public void cipherTextCombinesTextByColumn() {
95
-        Crypto crypto = new Crypto("First, solve the problem. Then, write the code.");
96
-        String expectedOutput = "foeewhilpmrervrticseohtottbeedshlnte";
21
+    public void lettersAreLowerCasedDuringEncryption() {
22
+        CryptoSquare cryptoSquare = new CryptoSquare("A");
23
+        String expectedOutput = "a";
97 24
 
98
-        assertEquals(expectedOutput, crypto.getCipherText());
25
+        assertEquals(expectedOutput, cryptoSquare.getCiphertext());
99 26
     }
100 27
 
101 28
     @Ignore("Remove to run test")
102 29
     @Test
103
-    public void cipherTextSkipsCellsWithNoText() {
104
-        Crypto crypto = new Crypto("Time is an illusion. Lunchtime doubly so.");
105
-        String expectedOutput = "tasneyinicdsmiohooelntuillibsuuml";
30
+    public void spacesAreRemovedDuringEncryption() {
31
+        CryptoSquare cryptoSquare = new CryptoSquare(" b ");
32
+        String expectedOutput = "b";
106 33
 
107
-        assertEquals(expectedOutput, crypto.getCipherText());
34
+        assertEquals(expectedOutput, cryptoSquare.getCiphertext());
108 35
     }
109 36
 
110 37
     @Ignore("Remove to run test")
111 38
     @Test
112
-    public void normalizedCipherTextIsSplitByHeightOfSquare() {
113
-        Crypto crypto = new Crypto("Vampires are people too!");
114
-        String expectedOutput = "vrel aepe mset paoo irpo";
39
+    public void punctuationIsRemovedDuringEncryption() {
40
+        CryptoSquare cryptoSquare = new CryptoSquare("@1,%!");
41
+        String expectedOutput = "1";
115 42
 
116
-        assertEquals(expectedOutput, crypto.getNormalizedCipherText());
43
+        assertEquals(expectedOutput, cryptoSquare.getCiphertext());
117 44
     }
118 45
 
119 46
     @Ignore("Remove to run test")
120 47
     @Test
121
-    public void normalizedCipherNotExactlyDivisibleBy5SpillsIntoSmallerSegment() {
122
-        Crypto crypto = new Crypto("Madness, and then illumination.");
123
-        String expectedOutput = "msemo aanin dnin ndla etlt shui";
48
+    public void nineCharacterPlaintextResultsInThreeChunksOfThreeCharacters() {
49
+        CryptoSquare cryptoSquare = new CryptoSquare("This is fun!");
50
+        String expectedOutput = "tsf hiu isn";
124 51
 
125
-        assertEquals(expectedOutput, crypto.getNormalizedCipherText());
52
+        assertEquals(expectedOutput, cryptoSquare.getCiphertext());
126 53
     }
127 54
 
128 55
     @Ignore("Remove to run test")
129 56
     @Test
130
-    public void normalizedCipherIsSplitIntoSegmentsOfCorrectSize() {
131
-        Crypto crypto = new Crypto("If man was meant to stay on the ground god would have given us roots");
132
-        String expectedOutput = "imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau";
57
+    public void eightCharacterPlaintextResultsInThreeChunksWithATrailingSpace() {
58
+        CryptoSquare cryptoSquare = new CryptoSquare("Chill out.");
59
+        String expectedOutput = "clu hlt io ";
133 60
 
134
-        assertEquals(expectedOutput, crypto.getNormalizedCipherText());
61
+        assertEquals(expectedOutput, cryptoSquare.getCiphertext());
135 62
     }
136 63
 
137 64
     @Ignore("Remove to run test")
138 65
     @Test
139
-    public void normalizedCipherTextIsSplitIntoSegmentsOfCorrectSizeWithPunctuation() {
140
-        Crypto crypto = new Crypto("Have a nice day. Feed the dog & chill out!");
141
-        String expectedOutput = "hifei acedl veeol eddgo aatcu nyhht";
66
+    public void fiftyFourCharacterPlaintextResultsInSevenChunksWithTrailingSpaces() {
67
+        CryptoSquare cryptoSquare = new CryptoSquare("If man was meant to stay on the ground, god would have given us roots.");
68
+        String expectedOutput = "imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn  sseoau ";
142 69
 
143
-        assertEquals(expectedOutput, crypto.getNormalizedCipherText());
70
+        assertEquals(expectedOutput, cryptoSquare.getCiphertext());
144 71
     }
145 72
 }