|
| 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 | +## Rust Installation |
| 62 | + |
| 63 | +Refer to the [exercism help page][help-page] for Rust installation and learning |
| 64 | +resources. |
| 65 | + |
| 66 | +## Writing the Code |
| 67 | + |
| 68 | +Execute the tests with: |
| 69 | + |
| 70 | +```bash |
| 71 | +$ cargo test |
| 72 | +``` |
| 73 | + |
| 74 | +All but the first test have been ignored. After you get the first test to |
| 75 | +pass, open the tests source file which is located in the `tests` directory |
| 76 | +and remove the `#[ignore]` flag from the next test and get the tests to pass |
| 77 | +again. Each separate test is a function with `#[test]` flag above it. |
| 78 | +Continue, until you pass every test. |
| 79 | + |
| 80 | +If you wish to run all tests without editing the tests source file, use: |
| 81 | + |
| 82 | +```bash |
| 83 | +$ cargo test -- --ignored |
| 84 | +``` |
| 85 | + |
| 86 | +To run a specific test, for example `some_test`, you can use: |
| 87 | + |
| 88 | +```bash |
| 89 | +$ cargo test some_test |
| 90 | +``` |
| 91 | + |
| 92 | +If the specific test is ignored use: |
| 93 | + |
| 94 | +```bash |
| 95 | +$ cargo test some_test -- --ignored |
| 96 | +``` |
| 97 | + |
| 98 | +To learn more about Rust tests refer to the [online test documentation][rust-tests] |
| 99 | + |
| 100 | +Make sure to read the [Modules](https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html) chapter if you |
| 101 | +haven't already, it will help you with organizing your files. |
| 102 | + |
| 103 | +## Feedback, Issues, Pull Requests |
| 104 | + |
| 105 | +The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help! |
| 106 | + |
| 107 | +If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md). |
| 108 | + |
| 109 | +[help-page]: https://exercism.io/tracks/rust/learning |
| 110 | +[modules]: https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html |
| 111 | +[cargo]: https://doc.rust-lang.org/book/2018-edition/ch14-00-more-about-cargo.html |
| 112 | +[rust-tests]: https://doc.rust-lang.org/book/2018-edition/ch11-02-running-tests.html |
| 113 | + |
| 114 | +## Source |
| 115 | + |
| 116 | +Wikipedia [https://en.wikipedia.org/wiki/Transposition_cipher#Rail_Fence_cipher](https://en.wikipedia.org/wiki/Transposition_cipher#Rail_Fence_cipher) |
| 117 | + |
| 118 | +## Submitting Incomplete Solutions |
| 119 | +It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
0 commit comments