Skip to content

Commit b304fee

Browse files
author
Piet van Dongen
committed
Added exercise atbash-cipher
1 parent 58f57b3 commit b304fee

6 files changed

Lines changed: 101 additions & 1 deletion

File tree

‎atbash-cipher/build.gradle‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

‎atbash-cipher/example.java‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

‎atbash-cipher/src/main/java/.keep‎

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
public class Atbash {
2+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

‎config.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"prime-factors",
2424
"raindrops",
2525
"allergies",
26-
"strain"
26+
"strain",
27+
"atbash-cipher"
2728
],
2829
"deprecated": [
2930
],

0 commit comments

Comments
 (0)