Преглед изворни кода

rail-fence-cipher: add to track (#765)

* Rail Fence Cipher completed

* Added @Ignore in the test file

* Build fail corrections

* Build Fail

* Added .keep file, the abscence caused build to fail

* Changes done as per the recent conventions

* Modified the config.json adding topics as well

* Update config.json

* rail-fence-encoding: add to config.json

* rail-fence-cipher: improve reference solution

* rail-fence-cipher: update with @FridaTveit's suggestions.
Leon D'souza пре 8 година
родитељ
комит
1cd5cdc3a4

+ 12
- 0
config.json Прегледај датотеку

@@ -876,6 +876,18 @@
876 876
     },
877 877
     {
878 878
       "core": false,
879
+      "difficulty": 6,
880
+      "slug": "rail-fence-cipher",
881
+      "topics": [
882
+        "strings",
883
+        "loops",
884
+        "conditionals"
885
+      ],
886
+      "unlocked_by": "rotational-cipher",
887
+      "uuid": "6e4ad4ed-cc02-4132-973d-b9163ba0ea3d"
888
+    },
889
+    {
890
+      "core": false,
879 891
       "difficulty": 7,
880 892
       "slug": "anagram",
881 893
       "topics": [

+ 64
- 0
exercises/rail-fence-cipher/.meta/src/reference/java/RailFenceCipher.java Прегледај датотеку

@@ -0,0 +1,64 @@
1
+import java.util.Arrays;
2
+
3
+class RailFenceCipher {
4
+
5
+    private int key;
6
+
7
+    RailFenceCipher(int key) {
8
+        this.key = key;
9
+    }
10
+
11
+    String getEncryptedData(String message) {
12
+        String[] lines = splitIntoLines(message, false);
13
+        StringBuilder result = new StringBuilder();
14
+        for (String line : lines) {
15
+            result.append(line);
16
+        }
17
+        return result.toString();
18
+    }
19
+
20
+    String getDecryptedData(String message) {
21
+        String[] lines = splitIntoLines(message, true);
22
+
23
+        int charCount = 0;
24
+        for (int i = 0; i < key; ++i) {
25
+            while (lines[i].contains("?")) {
26
+                String letter = String.valueOf(message.charAt(charCount));
27
+                lines[i] = lines[i].replaceFirst("\\?", letter);
28
+                charCount++;
29
+            }
30
+        }
31
+
32
+        StringBuilder result = new StringBuilder();
33
+        int lineCount = 0;
34
+        int direction = -1;
35
+        for (int i = 0; i < message.length(); ++i) {
36
+            String letter = String.valueOf(lines[lineCount].charAt(0));
37
+            lines[lineCount] = lines[lineCount].substring(1);
38
+            result.append(letter);
39
+            direction *= lineCount == 0 || lineCount == key - 1 ? -1 : 1;
40
+            lineCount += direction;
41
+        }
42
+        return result.toString();
43
+    }
44
+
45
+    private String[] splitIntoLines(String message, boolean encrypted) {
46
+        String[] result = generateEmptyStrings(key);
47
+        int lineCount = 0;
48
+        int direction = -1;
49
+        for (char c : message.toCharArray()) {
50
+            String letter = String.valueOf(c);
51
+            result[lineCount] += encrypted ? "?" : letter;
52
+            direction *= lineCount == 0 || lineCount == key - 1 ? -1 : 1;
53
+            lineCount += direction;
54
+        }
55
+        return result;
56
+    }
57
+
58
+    private String[] generateEmptyStrings(int num) {
59
+        String[] strings = new String[num];
60
+        Arrays.fill(strings, "");
61
+        return strings;
62
+    }
63
+
64
+}

+ 78
- 0
exercises/rail-fence-cipher/README.md Прегледај датотеку

@@ -0,0 +1,78 @@
1
+# Rail Fence Cipher
2
+
3
+Implement encoding and decoding for the rail fence cipher.
4
+
5
+The Rail Fence cipher is a form of transposition cipher that gets its name from
6
+the way in which it's encoded. It was already used by the ancient Greeks.
7
+
8
+In the Rail Fence cipher, the message is written downwards on successive "rails"
9
+of an imaginary fence, then moving up when we get to the bottom (like a zig-zag).
10
+Finally the message is then read off in rows.
11
+
12
+For example, using three "rails" and the message "WE ARE DISCOVERED FLEE AT ONCE",
13
+the cipherer writes out:
14
+
15
+```text
16
+W . . . E . . . C . . . R . . . L . . . T . . . E
17
+. E . R . D . S . O . E . E . F . E . A . O . C .
18
+. . A . . . I . . . V . . . D . . . E . . . N . .
19
+```
20
+
21
+Then reads off:
22
+
23
+```text
24
+WECRLTEERDSOEEFEAOCAIVDEN
25
+```
26
+
27
+To decrypt a message you take the zig-zag shape and fill the ciphertext along the rows.
28
+
29
+```text
30
+? . . . ? . . . ? . . . ? . . . ? . . . ? . . . ?
31
+. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? .
32
+. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .
33
+```
34
+
35
+The first row has seven spots that can be filled with "WECRLTE".
36
+
37
+```text
38
+W . . . E . . . C . . . R . . . L . . . T . . . E
39
+. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? .
40
+. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .
41
+```
42
+
43
+Now the 2nd row takes "ERDSOEEFEAOC".
44
+
45
+```text
46
+W . . . E . . . C . . . R . . . L . . . T . . . E
47
+. E . R . D . S . O . E . E . F . E . A . O . C .
48
+. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .
49
+```
50
+
51
+Leaving "AIVDEN" for the last row.
52
+
53
+```text
54
+W . . . E . . . C . . . R . . . L . . . T . . . E
55
+. E . R . D . S . O . E . E . F . E . A . O . C .
56
+. . A . . . I . . . V . . . D . . . E . . . N . .
57
+```
58
+
59
+If you now read along the zig-zag shape you can read the original message.
60
+
61
+
62
+
63
+To run the tests:
64
+
65
+```sh
66
+$ gradle test
67
+```
68
+
69
+For more detailed info about the Java track see the [help page](http://exercism.io/languages/java).
70
+
71
+
72
+## Source
73
+
74
+[Wikipedia](https://en.wikipedia.org/wiki/Transposition_cipher#Rail_Fence_cipher)
75
+
76
+## Submitting Incomplete Solutions
77
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
78
+i

+ 17
- 0
exercises/rail-fence-cipher/build.gradle Прегледај датотеку

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

+ 0
- 0
exercises/rail-fence-cipher/src/main/java/.keep Прегледај датотеку


+ 55
- 0
exercises/rail-fence-cipher/src/test/java/RailFenceCipherTest.java Прегледај датотеку

@@ -0,0 +1,55 @@
1
+import org.junit.Assert;
2
+import org.junit.Ignore;
3
+import org.junit.Test;
4
+
5
+public class RailFenceCipherTest {
6
+
7
+	private RailFenceCipher railFenceCipher;
8
+
9
+	@Test
10
+	public void encodeWithTwoRails() {
11
+		railFenceCipher = new RailFenceCipher(2);
12
+		Assert.assertEquals("XXXXXXXXXOOOOOOOOO", 
13
+			railFenceCipher.getEncryptedData("XOXOXOXOXOXOXOXOXO"));
14
+	}
15
+
16
+	@Ignore("Remove to run test")
17
+	@Test
18
+	public void encodeWithThreeRails() {
19
+		railFenceCipher = new RailFenceCipher(3);
20
+		Assert.assertEquals("WECRLTEERDSOEEFEAOCAIVDEN", 
21
+			railFenceCipher.getEncryptedData("WEAREDISCOVEREDFLEEATONCE"));
22
+	}
23
+
24
+	@Ignore("Remove to run test")
25
+	@Test
26
+	public void encodeWithEndingInTheMiddle() {
27
+		railFenceCipher = new RailFenceCipher(4);
28
+		Assert.assertEquals("ESXIEECSR", 
29
+			railFenceCipher.getEncryptedData("EXERCISES"));
30
+	}
31
+
32
+	@Ignore("Remove to run test")
33
+	@Test
34
+	public void decodeWithThreeRails() {
35
+		railFenceCipher = new RailFenceCipher(3);
36
+		Assert.assertEquals("THEDEVILISINTHEDETAILS", 
37
+			railFenceCipher.getDecryptedData("TEITELHDVLSNHDTISEIIEA"));
38
+	}
39
+
40
+	@Ignore("Remove to run test")
41
+	@Test
42
+	public void decodeWithFiveRails() {
43
+		railFenceCipher = new RailFenceCipher(5);
44
+		Assert.assertEquals("EXERCISMISAWESOME", 
45
+			railFenceCipher.getDecryptedData("EIEXMSMESAORIWSCE"));
46
+	}
47
+
48
+	@Ignore("Remove to run test")
49
+	@Test
50
+	public void decodeWithSixRails() {
51
+		railFenceCipher = new RailFenceCipher(6);
52
+		Assert.assertEquals("112358132134558914423337761098715972584418167651094617711286", 
53
+			railFenceCipher.getDecryptedData("133714114238148966225439541018335470986172518171757571896261"));
54
+	}
55
+}

+ 1
- 0
exercises/settings.gradle Прегледај датотеку

@@ -61,6 +61,7 @@ include 'protein-translation'
61 61
 include 'proverb'
62 62
 include 'pythagorean-triplet'
63 63
 include 'queen-attack'
64
+include 'rail-fence-cipher'
64 65
 include 'raindrops'
65 66
 include 'rectangles'
66 67
 include 'reverse-string'