-
-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rail-fence-cipher
exercise
#355
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
exercises/practice/rail-fence-cipher/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Instructions | ||
|
||
Implement encoding and decoding for the rail fence cipher. | ||
|
||
The Rail Fence cipher is a form of transposition cipher that gets its name from the way in which it's encoded. | ||
It was already used by the ancient Greeks. | ||
|
||
In the Rail Fence cipher, the message is written downwards on successive "rails" of an imaginary fence, then moving up when we get to the bottom (like a zig-zag). | ||
Finally the message is then read off in rows. | ||
|
||
For example, using three "rails" and the message "WE ARE DISCOVERED FLEE AT ONCE", the cipherer writes out: | ||
|
||
```text | ||
W . . . E . . . C . . . R . . . L . . . T . . . E | ||
. E . R . D . S . O . E . E . F . E . A . O . C . | ||
. . A . . . I . . . V . . . D . . . E . . . N . . | ||
``` | ||
|
||
Then reads off: | ||
|
||
```text | ||
WECRLTEERDSOEEFEAOCAIVDEN | ||
``` | ||
|
||
To decrypt a message you take the zig-zag shape and fill the ciphertext along the rows. | ||
|
||
```text | ||
? . . . ? . . . ? . . . ? . . . ? . . . ? . . . ? | ||
. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . | ||
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . . | ||
``` | ||
|
||
The first row has seven spots that can be filled with "WECRLTE". | ||
|
||
```text | ||
W . . . E . . . C . . . R . . . L . . . T . . . E | ||
. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . | ||
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . . | ||
``` | ||
|
||
Now the 2nd row takes "ERDSOEEFEAOC". | ||
|
||
```text | ||
W . . . E . . . C . . . R . . . L . . . T . . . E | ||
. E . R . D . S . O . E . E . F . E . A . O . C . | ||
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . . | ||
``` | ||
|
||
Leaving "AIVDEN" for the last row. | ||
|
||
```text | ||
W . . . E . . . C . . . R . . . L . . . T . . . E | ||
. E . R . D . S . O . E . E . F . E . A . O . C . | ||
. . A . . . I . . . V . . . D . . . E . . . N . . | ||
``` | ||
|
||
If you now read along the zig-zag shape you can read the original message. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"erikschierboom" | ||
], | ||
"files": { | ||
"solution": [ | ||
"rail-fence-cipher.R" | ||
], | ||
"test": [ | ||
"test_rail-fence-cipher.R" | ||
], | ||
"example": [ | ||
".meta/example.R" | ||
] | ||
}, | ||
"blurb": "Implement encoding and decoding for the rail fence cipher.", | ||
"source": "Wikipedia", | ||
"source_url": "https://en.wikipedia.org/wiki/Transposition_cipher#Rail_Fence_cipher" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
encode <- function(rails, plaintext) { | ||
transform(rails, plaintext, \(i, fence) fence[i]) | ||
} | ||
|
||
decode <- function(rails, ciphertext) { | ||
transform(rails, ciphertext, \(i, fence) match(i, fence)) | ||
} | ||
|
||
transform <- function(rails, text, char_index) { | ||
chars <- strsplit(text, "") |> unlist() | ||
fence <- rail_fence(rails, length(chars)) | ||
indices <- sapply(seq_along(chars), \(i) char_index(i, fence)) | ||
chars[indices] |> paste0(collapse = "") | ||
} | ||
|
||
rail_fence <- function(rails, num_chars) { | ||
zigzag <- c(seq_len(rails), seq(rails - 1, 2)) | ||
rep(zigzag, length.out = num_chars) |> order() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[46dc5c50-5538-401d-93a5-41102680d068] | ||
description = "encode -> encode with two rails" | ||
|
||
[25691697-fbd8-4278-8c38-b84068b7bc29] | ||
description = "encode -> encode with three rails" | ||
|
||
[384f0fea-1442-4f1a-a7c4-5cbc2044002c] | ||
description = "encode -> encode with ending in the middle" | ||
|
||
[cd525b17-ec34-45ef-8f0e-4f27c24a7127] | ||
description = "decode -> decode with three rails" | ||
|
||
[dd7b4a98-1a52-4e5c-9499-cbb117833507] | ||
description = "decode -> decode with five rails" | ||
|
||
[93e1ecf4-fac9-45d9-9cd2-591f47d3b8d3] | ||
description = "decode -> decode with six rails" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
encode <- function(rails, plaintext) { | ||
|
||
} | ||
|
||
decode <- function(rails, ciphertext) { | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
exercises/practice/rail-fence-cipher/test_rail-fence-cipher.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
source("./rail-fence-cipher.R") | ||
library(testthat) | ||
|
||
test_that("Encode with two rails", { | ||
rails <- 2 | ||
plaintext <- "XOXOXOXOXOXOXOXOXO" | ||
expected <- "XXXXXXXXXOOOOOOOOO" | ||
expect_equal(encode(rails, plaintext), expected) | ||
}) | ||
|
||
test_that("Encode with three rails", { | ||
rails <- 3 | ||
plaintext <- "WEAREDISCOVEREDFLEEATONCE" | ||
expected <- "WECRLTEERDSOEEFEAOCAIVDEN" | ||
expect_equal(encode(rails, plaintext), expected) | ||
}) | ||
|
||
test_that("Encode with ending in the middle", { | ||
rails <- 4 | ||
plaintext <- "EXERCISES" | ||
expected <- "ESXIEECSR" | ||
expect_equal(encode(rails, plaintext), expected) | ||
}) | ||
|
||
test_that("Decode with three rails", { | ||
rails <- 3 | ||
ciphertext <- "TEITELHDVLSNHDTISEIIEA" | ||
expected <- "THEDEVILISINTHEDETAILS" | ||
expect_equal(decode(rails, ciphertext), expected) | ||
}) | ||
|
||
test_that("Decode with five rails", { | ||
rails <- 5 | ||
ciphertext <- "EIEXMSMESAORIWSCE" | ||
expected <- "EXERCISMISAWESOME" | ||
expect_equal(decode(rails, ciphertext), expected) | ||
}) | ||
|
||
test_that("Decode with six rails", { | ||
rails <- 6 | ||
ciphertext <- "133714114238148966225439541018335470986172518171757571896261" | ||
expected <- "112358132134558914423337761098715972584418167651094617711286" | ||
expect_equal(decode(rails, ciphertext), expected) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm obviously biased, but I think this example implementation is one of the nicest I've seen. R is so powerful!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not going to disagree!