Skip to content

Commit 9917119

Browse files
committed
rail-fence-cipher: update with @FridaTveit's suggestions.
1 parent 909418f commit 9917119

3 files changed

Lines changed: 96 additions & 18 deletions

File tree

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@
492492
"loops",
493493
"conditionals"
494494
],
495-
"unlocked_by": "atbash-cipher",
495+
"unlocked_by": "rotational-cipher",
496496
"uuid": "6e4ad4ed-cc02-4132-973d-b9163ba0ea3d"
497497
},
498498
{

exercises/rail-fence-cipher/.meta/src/reference/java/RailFenceCipher.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
public class RailFenceCipher {
1+
import java.util.Arrays;
22

3-
private static int key;
3+
class RailFenceCipher {
44

5-
public RailFenceCipher(int key) {
6-
RailFenceCipher.key = key;
5+
private int key;
6+
7+
RailFenceCipher(int key) {
8+
this.key = key;
79
}
810

9-
public static String getEncryptedData(String message) {
11+
String getEncryptedData(String message) {
1012
String[] lines = splitIntoLines(message, false);
11-
String result = "";
13+
StringBuilder result = new StringBuilder();
1214
for (String line : lines) {
13-
result += line;
15+
result.append(line);
1416
}
15-
return result;
17+
return result.toString();
1618
}
1719

18-
public static String getDecryptedData(String message) {
20+
String getDecryptedData(String message) {
1921
String[] lines = splitIntoLines(message, true);
2022

2123
int charCount = 0;
@@ -27,20 +29,20 @@ public static String getDecryptedData(String message) {
2729
}
2830
}
2931

30-
String result = "";
32+
StringBuilder result = new StringBuilder();
3133
int lineCount = 0;
3234
int direction = -1;
3335
for (int i = 0; i < message.length(); ++i) {
3436
String letter = String.valueOf(lines[lineCount].charAt(0));
3537
lines[lineCount] = lines[lineCount].substring(1);
36-
result += letter;
38+
result.append(letter);
3739
direction *= lineCount == 0 || lineCount == key - 1 ? -1 : 1;
3840
lineCount += direction;
3941
}
40-
return result;
42+
return result.toString();
4143
}
4244

43-
private static String[] splitIntoLines(String message, boolean encrypted) {
45+
private String[] splitIntoLines(String message, boolean encrypted) {
4446
String[] result = generateEmptyStrings(key);
4547
int lineCount = 0;
4648
int direction = -1;
@@ -53,11 +55,9 @@ private static String[] splitIntoLines(String message, boolean encrypted) {
5355
return result;
5456
}
5557

56-
private static String[] generateEmptyStrings(int num) {
58+
private String[] generateEmptyStrings(int num) {
5759
String[] strings = new String[num];
58-
for (int i = 0; i < num; ++i) {
59-
strings[i] = "";
60-
}
60+
Arrays.fill(strings, "");
6161
return strings;
6262
}
6363

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

0 commit comments

Comments
 (0)