ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# SergiiVlasiuk's solution

## to Rail Fence Cipher in the Scala Track

Published at Aug 26 2019 · 0 comments
Instructions
Test suite
Solution

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:

``````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 . .
``````

``````WECRLTEERDSOEEFEAOCAIVDEN
``````

To decrypt a message you take the zig-zag shape and fill the ciphertext along the rows.

``````? . . . ? . . . ? . . . ? . . . ? . . . ? . . . ?
. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? .
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .
``````

The first row has seven spots that can be filled with "WECRLTE".

``````W . . . E . . . C . . . R . . . L . . . T . . . E
. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? .
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .
``````

Now the 2nd row takes "ERDSOEEFEAOC".

``````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.

``````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.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test` in the exercise directory.

For more detailed info about the Scala track see the help page.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### RailFenceCipherTest.scala

``````import org.scalatest.{Matchers, FunSuite}

/** @version 1.1.0 */
class RailFenceCipherTest extends FunSuite with Matchers {

test("encode with two rails") {
RailFenceCipher.encode("XOXOXOXOXOXOXOXOXO", 2) should be(
"XXXXXXXXXOOOOOOOOO")
}

test("encode with three rails") {
pending
RailFenceCipher.encode("WEAREDISCOVEREDFLEEATONCE", 3) should be(
"WECRLTEERDSOEEFEAOCAIVDEN")
}

test("encode with ending in the middle") {
pending
RailFenceCipher.encode("EXERCISES", 4) should be("ESXIEECSR")
}

test("decode with three rails") {
pending
RailFenceCipher.decode("TEITELHDVLSNHDTISEIIEA", 3) should be(
"THEDEVILISINTHEDETAILS")
}

test("decode with five rails") {
pending
RailFenceCipher.decode("EIEXMSMESAORIWSCE", 5) should be(
"EXERCISMISAWESOME")
}

test("decode with six rails") {
pending
RailFenceCipher.decode(
"133714114238148966225439541018335470986172518171757571896261",
6) should be(
"112358132134558914423337761098715972584418167651094617711286")
}
}``````
``````object RailFenceCipher {
def encode(text: String, howManyRails: Int): String =
cipher(text.length, howManyRails) map { i => text(i._1) } mkString

def decode(cipherText: String, howManyRails: Int): String =
cipher(cipherText.length, howManyRails) sortBy (_._1) map { i => cipherText(i._2) } mkString

private def cipher(textLength: Int, howManyRails: Int): Seq[(Int, Int)] = {
val railIndices = makeRailIndices(howManyRails) zip (0 until textLength)
railIndices.sortBy(_._1).zipWithIndex.map {
case ((railId, encodeIndex), decodeIndex) => (encodeIndex, decodeIndex)
}
}

private def makeRailIndices(n: Int): Stream[Int] = {
val xs = (1 to n).toList
val ys = xs ++ xs.reverse.tail.init
Stream.continually(ys.toStream).flatten
}
}``````

### What can you learn from this solution?

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?