Skip to content

Commit 1cd5cdc

Browse files
LeonDsouzaFridaTveit
authored andcommitted
rail-fence-cipher: add to track (exercism#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.
1 parent 8eae37b commit 1cd5cdc

7 files changed

Lines changed: 227 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,18 @@
874874
"unlocked_by": "bank-account",
875875
"uuid": "38a405e8-619d-400f-b53c-2f06461fdf9d"
876876
},
877+
{
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+
},
877889
{
878890
"core": false,
879891
"difficulty": 7,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
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+
}

‎exercises/rail-fence-cipher/src/main/java/.keep‎

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

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ include 'protein-translation'
6161
include 'proverb'
6262
include 'pythagorean-triplet'
6363
include 'queen-attack'
64+
include 'rail-fence-cipher'
6465
include 'raindrops'
6566
include 'rectangles'
6667
include 'reverse-string'

0 commit comments

Comments
 (0)