Ver código fonte

Rotational Cipher add to track

Leon D'souza 9 anos atrás
pai
commit
8e30cdaa6e

+ 7
- 0
config.json Ver arquivo

@@ -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 Ver arquivo

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

BIN
exercises/rotational-cipher/src/example/java/RotationalCipher.class Ver arquivo


+ 44
- 0
exercises/rotational-cipher/src/example/java/RotationalCipher.java Ver arquivo

@@ -0,0 +1,44 @@
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
+     * 
26
+     * @param characterToReplace
27
+     * @param alphabetCaseStart
28
+     * @param alphabetCaseEnd
29
+     * @param shiftKey
30
+     * @return ReplacementCharacter
31
+     * 
32
+     * For Uppercase CaseStart = 'A' and CaseEnd = 'Z'
33
+     * For Lowercase CaseStart = 'a' and CaseEnd = 'z'
34
+     */
35
+    public char getReplacementCharacter(char characterToReplace,
36
+            char alphabetCaseStart, char alphabetCaseEnd) {
37
+        char replacementCharacter = (char) (characterToReplace + shiftKey);
38
+        if (replacementCharacter > alphabetCaseEnd) {
39
+            replacementCharacter = (char) ((alphabetCaseStart - 1) +
40
+             (replacementCharacter % alphabetCaseEnd));
41
+        }
42
+        return replacementCharacter;
43
+    }
44
+}

+ 0
- 0
exercises/rotational-cipher/src/main/java/.keep Ver arquivo


+ 86
- 0
exercises/rotational-cipher/src/test/java/RotationalCipherTest.java Ver arquivo

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

+ 1
- 0
exercises/settings.gradle Ver arquivo

@@ -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'