Selaa lähdekoodia

Merge pull request #36 from exercism/atbash-cipher

Added exercise atbash-cipher
Piet van Dongen 11 vuotta sitten
vanhempi
commit
3ffa8ed97c

+ 11
- 0
atbash-cipher/build.gradle Näytä tiedosto

@@ -0,0 +1,11 @@
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.10"
11
+}

+ 48
- 0
atbash-cipher/example.java Näytä tiedosto

@@ -0,0 +1,48 @@
1
+import java.util.ArrayList;
2
+import java.util.List;
3
+
4
+public class Atbash {
5
+
6
+    private static final int GROUP_SIZE = 5;
7
+    private static final String PLAIN = "abcdefghijklmnopqrstuvwxyz";
8
+    private static final String CIPHER = "zyxwvutsrqponmlkjihgfedcba";
9
+
10
+    public static String encode(String input) {
11
+        String encoded = stripInvalidCharacters(input).toLowerCase();
12
+        String cyphered = "";
13
+
14
+        for (char c : encoded.toCharArray()) {
15
+            cyphered += applyCipher(c);
16
+        }
17
+
18
+        return splitIntoFiveLetterWords(cyphered);
19
+    }
20
+
21
+    private static String stripInvalidCharacters(String input) {
22
+        String filteredValue = "";
23
+
24
+        for (char c : input.toCharArray()) {
25
+            if (Character.isLetterOrDigit(c)) {
26
+                filteredValue += c;
27
+            }
28
+        }
29
+
30
+        return filteredValue;
31
+    }
32
+
33
+    private static char applyCipher(char input) {
34
+        int idx = PLAIN.indexOf(input);
35
+
36
+        return idx >= 0 ? CIPHER.toCharArray()[idx] : input;
37
+    }
38
+
39
+    private static String splitIntoFiveLetterWords(String value) {
40
+        List<String> words = new ArrayList<>();
41
+
42
+        for (int i = 0; i < value.length(); i += GROUP_SIZE) {
43
+            words.add(i + GROUP_SIZE <= value.length() ? value.substring(i, i + GROUP_SIZE) : value.substring(i));
44
+        }
45
+
46
+        return String.join(" ", words);
47
+    }
48
+}

+ 0
- 0
atbash-cipher/src/main/java/.keep Näytä tiedosto


+ 2
- 0
atbash-cipher/src/main/java/Atbash.java Näytä tiedosto

@@ -0,0 +1,2 @@
1
+public class Atbash {
2
+}

+ 38
- 0
atbash-cipher/src/test/java/AtbashTest.java Näytä tiedosto

@@ -0,0 +1,38 @@
1
+import org.junit.Test;
2
+import org.junit.runner.RunWith;
3
+import org.junit.runners.Parameterized;
4
+
5
+import java.util.Arrays;
6
+import java.util.Collection;
7
+
8
+import static org.junit.Assert.assertEquals;
9
+
10
+@RunWith(Parameterized.class)
11
+public class AtbashTest {
12
+
13
+    private String input;
14
+    private String expectedOutput;
15
+
16
+    @Parameterized.Parameters
17
+    public static Collection<Object[]> data() {
18
+        return Arrays.asList(new Object[][]{
19
+                {"no", "ml"},
20
+                {"yes", "bvh"},
21
+                {"OMG", "lnt"},
22
+                {"mindblowingly", "nrmwy oldrm tob"},
23
+                {"Testing, 1 2 3, testing.", "gvhgr mt123 gvhgr mt"},
24
+                {"Truth is fiction.", "gifgs rhurx grlm"},
25
+                {"The quick brown fox jumps over the lazy dog.", "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"}
26
+        });
27
+    }
28
+
29
+    public AtbashTest(String input, String expectedOutput) {
30
+        this.input = input;
31
+        this.expectedOutput = expectedOutput;
32
+    }
33
+
34
+    @Test
35
+    public void test() {
36
+        assertEquals(expectedOutput, Atbash.encode(input));
37
+    }
38
+}

+ 2
- 1
config.json Näytä tiedosto

@@ -23,7 +23,8 @@
23 23
     "prime-factors",
24 24
     "raindrops",
25 25
     "allergies",
26
-    "strain"
26
+    "strain",
27
+    "atbash-cipher"
27 28
   ],
28 29
   "deprecated": [
29 30
   ],