3 kyu

Rail Fence Cipher: Encoding and Decoding

2,068 of 8,989darrentburgess

Description:

Create two functions to encode and then decode a string using the Rail Fence Cipher. This cipher is used to encode a string by placing each character successively in a diagonal along a set of "rails". First start off moving diagonally and down. When you reach the bottom, reverse direction and move diagonally and up until you reach the top rail. Continue until you reach the end of the string. Each "rail" is then read left to right to derive the encoded string.

For example, the string "WEAREDISCOVEREDFLEEATONCE" could be represented in a three rail system as follows:

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    

The encoded string would be:

WECRLTEERDSOEEFEAOCAIVDEN

Write a function/method that takes 2 arguments, a string and the number of rails, and returns the ENCODED string.

Write a second function/method that takes 2 arguments, an encoded string and the number of rails, and returns the DECODED string.

For both encoding and decoding, assume number of rails >= 2 and that passing an empty string will return an empty string.

Note that the example above excludes the punctuation and spaces just for simplicity. There are, however, tests that include punctuation. Don't filter out punctuation as they are a part of the string.

Algorithms
Ciphers
Cryptography
Strings
Security

Stats:

CreatedMar 12, 2017
PublishedMar 15, 2017
Warriors Trained24077
Total Skips5896
Total Code Submissions71539
Total Times Completed8989
JavaScript Completions2068
Ruby Completions289
Python Completions3635
Java Completions901
Haskell Completions142
C++ Completions843
C# Completions659
Go Completions248
Rust Completions228
PHP Completions181
COBOL Completions8
VB Completions7
Total Stars1126
% of votes with a positive feedback rating96% of 1286
Total "Very Satisfied" Votes1187
Total "Somewhat Satisfied" Votes90
Total "Not Satisfied" Votes9
Total Rank Assessments9
Average Assessed Rank
3 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
4 kyu
Ad
Contributors
  • darrentburgess Avatar
  • imjasonmiller Avatar
  • user5036852 Avatar
  • docgunthrop Avatar
  • JohanWiltink Avatar
  • Blind4Basics Avatar
  • njohnson7 Avatar
  • Firefly2002 Avatar
  • KataSideKick Avatar
  • FArekkusu Avatar
  • hobovsky Avatar
  • user8436785 Avatar
  • aidvu Avatar
  • akar-0 Avatar
  • dfhwze Avatar
  • qpBtJ8VrhMLXoXvoovXn Avatar
Ad