Skip to content

Commit 8e30cda

Browse files
committed
Rotational Cipher add to track
1 parent 5f7886a commit 8e30cda

7 files changed

Lines changed: 155 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818

1919
]
2020
},
21+
{
22+
"slug": "rotational-cipher",
23+
"difficulty": 1,
24+
"topics": [
25+
26+
]
27+
},
2128
{
2229
"slug": "rna-transcription",
2330
"difficulty": 2,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
1009 Bytes
Binary file not shown.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

‎exercises/rotational-cipher/src/main/java/.keep‎

Whitespace-only changes.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ include 'robot-name'
6060
include 'robot-simulator'
6161
include 'roman-numerals'
6262
include 'run-length-encoding'
63+
include 'rotational-cipher'
6364
include 'saddle-points'
6465
include 'scrabble-score'
6566
include 'secret-handshake'

0 commit comments

Comments
 (0)