-
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add practice exercise Rail Fence Cipher (#881)
- Loading branch information
Showing
12 changed files
with
18,245 additions
and
0 deletions.
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,21 @@ | ||
{ | ||
"authors": [ | ||
"klimkin" | ||
], | ||
"files": { | ||
"solution": [ | ||
"rail_fence_cipher.cpp", | ||
"rail_fence_cipher.h" | ||
], | ||
"test": [ | ||
"rail_fence_cipher_test.cpp" | ||
], | ||
"example": [ | ||
".meta/example.cpp", | ||
".meta/example.h" | ||
] | ||
}, | ||
"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,49 @@ | ||
#include "rail_fence_cipher.h" | ||
|
||
namespace rail_fence_cipher { | ||
|
||
std::string encode(const std::string& plaintext, int num_rails) { | ||
if (num_rails <= 1) return plaintext; | ||
|
||
const auto stride = num_rails * 2 - 2; | ||
const auto length = plaintext.size(); | ||
|
||
std::string result(length, ' '); | ||
auto dst = result.begin(); | ||
|
||
for (int rail = 0; rail < num_rails; ++rail) { | ||
for (size_t pos = rail; pos < length; pos += stride) { | ||
*dst++ = plaintext[pos]; | ||
if (0 < rail && rail < num_rails - 1) { | ||
size_t next_pos = pos + stride - rail * 2; | ||
if (next_pos < length) *dst++ = plaintext[next_pos]; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
std::string decode(const std::string& ciphertext, int num_rails) { | ||
if (num_rails <= 1) return ciphertext; | ||
|
||
const auto stride = num_rails * 2 - 2; | ||
const auto length = ciphertext.size(); | ||
|
||
std::string result(length, ' '); | ||
auto src = ciphertext.cbegin(); | ||
|
||
for (int rail = 0; rail < num_rails; ++rail) { | ||
for (size_t pos = rail; pos < length; pos += stride) { | ||
result[pos] = *src++; | ||
if (0 < rail && rail < num_rails - 1) { | ||
size_t next_pos = pos + stride - rail * 2; | ||
if (next_pos < length) result[next_pos] = *src++; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
} // namespace 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,10 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace rail_fence_cipher { | ||
|
||
std::string encode(const std::string& plaintext, int num_rails); | ||
std::string decode(const std::string& ciphertext, int num_rails); | ||
|
||
} // namespace 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,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,64 @@ | ||
# Get the exercise name from the current directory | ||
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME) | ||
|
||
# Basic CMake project | ||
cmake_minimum_required(VERSION 3.5.1) | ||
|
||
# Name the project after the exercise | ||
project(${exercise} CXX) | ||
|
||
# Get a source filename from the exercise name by replacing -'s with _'s | ||
string(REPLACE "-" "_" file ${exercise}) | ||
|
||
# Implementation could be only a header | ||
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp) | ||
set(exercise_cpp ${file}.cpp) | ||
else() | ||
set(exercise_cpp "") | ||
endif() | ||
|
||
# Use the common Catch library? | ||
if(EXERCISM_COMMON_CATCH) | ||
# For Exercism track development only | ||
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>) | ||
elseif(EXERCISM_TEST_SUITE) | ||
# The Exercism test suite is being run, the Docker image already | ||
# includes a pre-built version of Catch. | ||
find_package(Catch2 REQUIRED) | ||
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h) | ||
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain) | ||
# When Catch is installed system wide we need to include a different | ||
# header, we need this define to use the correct one. | ||
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE) | ||
else() | ||
# Build executable from sources and headers | ||
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp) | ||
endif() | ||
|
||
set_target_properties(${exercise} PROPERTIES | ||
CXX_STANDARD 17 | ||
CXX_STANDARD_REQUIRED OFF | ||
CXX_EXTENSIONS OFF | ||
) | ||
|
||
set(CMAKE_BUILD_TYPE Debug) | ||
|
||
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)") | ||
set_target_properties(${exercise} PROPERTIES | ||
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror" | ||
) | ||
endif() | ||
|
||
# Configure to run all the tests? | ||
if(${EXERCISM_RUN_ALL_TESTS}) | ||
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS) | ||
endif() | ||
|
||
# Tell MSVC not to warn us about unchecked iterators in debug builds | ||
if(${MSVC}) | ||
set_target_properties(${exercise} PROPERTIES | ||
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS) | ||
endif() | ||
|
||
# Run the tests on every build | ||
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise}) |
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,9 @@ | ||
#include "rail_fence_cipher.h" | ||
|
||
namespace rail_fence_cipher { | ||
|
||
std::string encode(const std::string& plaintext, int num_rails) {} | ||
|
||
std::string decode(const std::string& ciphertext, int num_rails) {} | ||
|
||
} // namespace 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,10 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace rail_fence_cipher { | ||
|
||
std::string encode(const std::string& plaintext, int num_rails); | ||
std::string decode(const std::string& ciphertext, int num_rails); | ||
|
||
} // namespace rail_fence_cipher |
42 changes: 42 additions & 0 deletions
42
exercises/practice/rail-fence-cipher/rail_fence_cipher_test.cpp
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,42 @@ | ||
#include "rail_fence_cipher.h" | ||
#ifdef EXERCISM_TEST_SUITE | ||
#include <catch2/catch.hpp> | ||
#else | ||
#include "test/catch.hpp" | ||
#endif | ||
|
||
TEST_CASE("encode with two rails", "[46dc5c50-5538-401d-93a5-41102680d068]") { | ||
REQUIRE(rail_fence_cipher::encode("XOXOXOXOXOXOXOXOXO", 2) == | ||
"XXXXXXXXXOOOOOOOOO"); | ||
} | ||
|
||
#if defined(EXERCISM_RUN_ALL_TESTS) | ||
|
||
TEST_CASE("encode with three rails", "[25691697-fbd8-4278-8c38-b84068b7bc29]") { | ||
REQUIRE(rail_fence_cipher::encode("WEAREDISCOVEREDFLEEATONCE", 3) == | ||
"WECRLTEERDSOEEFEAOCAIVDEN"); | ||
} | ||
|
||
TEST_CASE("encode with ending in the middle", | ||
"[384f0fea-1442-4f1a-a7c4-5cbc2044002c]") { | ||
REQUIRE(rail_fence_cipher::encode("EXERCISES", 4) == "ESXIEECSR"); | ||
} | ||
|
||
TEST_CASE("decode_with_three_rails", "[cd525b17-ec34-45ef-8f0e-4f27c24a7127]") { | ||
REQUIRE(rail_fence_cipher::decode("TEITELHDVLSNHDTISEIIEA", 3) == | ||
"THEDEVILISINTHEDETAILS"); | ||
} | ||
|
||
TEST_CASE("decode_with_five_rails", "[dd7b4a98-1a52-4e5c-9499-cbb117833507]") { | ||
REQUIRE(rail_fence_cipher::decode("EIEXMSMESAORIWSCE", 5) == | ||
"EXERCISMISAWESOME"); | ||
} | ||
|
||
TEST_CASE("decode_with_six_rails", "[93e1ecf4-fac9-45d9-9cd2-591f47d3b8d3]") { | ||
REQUIRE(rail_fence_cipher::decode( | ||
"133714114238148966225439541018335470986172518171757571896261", | ||
6) == | ||
"112358132134558914423337761098715972584418167651094617711286"); | ||
} | ||
|
||
#endif |
Oops, something went wrong.