Przeglądaj źródła

Merge pull request #695 from LeonDsouza/master

rotational-cipher add to track
FridaTveit 9 lat temu
rodzic
commit
687c0543b6

+ 7
- 0
config.json Wyświetl plik

@@ -19,6 +19,13 @@
19 19
       ]
20 20
     },
21 21
     {
22
+      "slug": "rotational-cipher",
23
+      "difficulty": 1,
24
+      "topics": [
25
+
26
+      ]
27
+    },
28
+    {
22 29
       "slug": "rna-transcription",
23 30
       "difficulty": 2,
24 31
       "topics": [

+ 17
- 0
exercises/rotational-cipher/build.gradle Wyświetl plik

@@ -0,0 +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
+}

+ 37
- 0
exercises/rotational-cipher/src/example/java/RotationalCipher.java Wyświetl plik

@@ -0,0 +1,37 @@
1
+
2
+public class RotationalCipher {
3
+
4
+    int shiftKey;
5
+
6
+    RotationalCipher(int shiftKey) {
7
+        this.shiftKey = shiftKey;
8
+    }
9
+
10
+    public String rotate(String data) {
11
+        StringBuilder dataStringBuilder = new StringBuilder();
12
+        for (char c : data.toCharArray()) {
13
+            if (Character.isUpperCase(c)) {
14
+                dataStringBuilder.append(getReplacementCharacter(c, 'A', 'Z'));
15
+            } else if (Character.isLowerCase(c)) {
16
+                dataStringBuilder.append(getReplacementCharacter(c, 'a', 'z'));
17
+            } else {
18
+                dataStringBuilder.append(c);
19
+            }
20
+        }
21
+        return dataStringBuilder.toString();
22
+    }
23
+
24
+    /**
25
+     * For Uppercase CaseStart = 'A' and CaseEnd = 'Z'
26
+     * For Lowercase CaseStart = 'a' and CaseEnd = 'z'
27
+     */
28
+    private char getReplacementCharacter(char characterToReplace,
29
+            char alphabetCaseStart, char alphabetCaseEnd) {
30
+        char replacementCharacter = (char) (characterToReplace + shiftKey);
31
+        if (replacementCharacter > alphabetCaseEnd) {
32
+            replacementCharacter = (char) ((alphabetCaseStart - 1) +
33
+             (replacementCharacter % alphabetCaseEnd));
34
+        }
35
+        return replacementCharacter;
36
+    }
37
+}

+ 0
- 0
exercises/rotational-cipher/src/main/java/.keep Wyświetl plik


+ 81
- 0
exercises/rotational-cipher/src/test/java/RotationalCipherTest.java Wyświetl plik

@@ -0,0 +1,81 @@
1
+
2
+import org.junit.Assert;
3
+import org.junit.Before;
4
+import org.junit.Ignore;
5
+import org.junit.Test;
6
+
7
+public class RotationalCipherTest {
8
+
9
+    private RotationalCipher rotationalCipher;
10
+
11
+    @Test
12
+    public void rotateSingleCharacterBy1() {
13
+        rotationalCipher = new RotationalCipher(1);
14
+        Assert.assertEquals("b", rotationalCipher.rotate("a"));
15
+    }
16
+    
17
+    @Ignore("Remove to run test")
18
+    @Test
19
+    public void rotateSingleCharacterBy26() {
20
+        rotationalCipher = new RotationalCipher(26);
21
+        Assert.assertEquals("a", rotationalCipher.rotate("a"));
22
+    }
23
+
24
+    @Ignore("Remove to run test")
25
+    @Test
26
+    public void rotateSingleCharacterBy0() {
27
+        rotationalCipher = new RotationalCipher(0);
28
+        Assert.assertEquals("a", rotationalCipher.rotate("a"));
29
+    }
30
+
31
+    @Ignore("Remove to run test")
32
+    @Test
33
+    public void rotateSingleCharacterBy13() {
34
+        rotationalCipher = new RotationalCipher(13);
35
+        Assert.assertEquals("z", rotationalCipher.rotate("m"));
36
+    }
37
+
38
+    @Ignore("Remove to run test")
39
+    @Test
40
+    public void rotateSingleCharacterWithWrapAround() {
41
+        rotationalCipher = new RotationalCipher(13);
42
+        Assert.assertEquals("a", rotationalCipher.rotate("n"));
43
+    }
44
+
45
+    @Ignore("Remove to run test")
46
+    @Test
47
+    public void rotateCapitalLetters() {
48
+        rotationalCipher = new RotationalCipher(5);
49
+        Assert.assertEquals("TRL", rotationalCipher.rotate("OMG"));
50
+    }
51
+
52
+    @Ignore("Remove to run test")
53
+    @Test
54
+    public void rotateSpaces() {
55
+        rotationalCipher = new RotationalCipher(5);
56
+        Assert.assertEquals("T R L", rotationalCipher.rotate("O M G"));
57
+    }
58
+
59
+    @Ignore("Remove to run test")
60
+    @Test
61
+    public void rotateNumbers() {
62
+        rotationalCipher = new RotationalCipher(4);
63
+        Assert.assertEquals("Xiwxmrk 1 2 3 xiwxmrk", rotationalCipher.rotate("Testing 1 2 3 testing"));
64
+    }
65
+
66
+    @Ignore("Remove to run test")
67
+    @Test
68
+    public void rotatePunctuation() {
69
+        rotationalCipher = new RotationalCipher(21);
70
+        Assert.assertEquals("Gzo'n zvo, Bmviyhv!", rotationalCipher.rotate("Let's eat, Grandma!"));
71
+    }
72
+
73
+    @Ignore("Remove to run test")
74
+    @Test
75
+    public void rotateAllLetters() {
76
+        rotationalCipher = new RotationalCipher(13);
77
+        Assert.assertEquals("The quick brown fox jumps over the lazy dog.",
78
+         rotationalCipher.rotate("Gur dhvpx oebja sbk whzcf bire gur ynml qbt."));
79
+    } 
80
+
81
+}

+ 1
- 0
exercises/settings.gradle Wyświetl plik

@@ -60,6 +60,7 @@ include 'robot-name'
60 60
 include 'robot-simulator'
61 61
 include 'roman-numerals'
62 62
 include 'run-length-encoding'
63
+include 'rotational-cipher'
63 64
 include 'saddle-points'
64 65
 include 'scrabble-score'
65 66
 include 'secret-handshake'