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

# SergiiVlasiuk's solution

## to Rotational Cipher in the Scala Track

Published at Sep 01 2019 · 0 comments
Instructions
Test suite
Solution

Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.

The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an integer key between `0` and `26`. Using a key of `0` or `26` will always yield the same output due to modular arithmetic. The letter is shifted for as many values as the value of the key.

The general notation for rotational ciphers is `ROT + <key>`. The most commonly used rotational cipher is `ROT13`.

A `ROT13` on the Latin alphabet would be as follows:

``````Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: nopqrstuvwxyzabcdefghijklm
``````

It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys.

Ciphertext is written out in the same formatting as the input including spaces and punctuation.

## Examples

• ROT5 `omg` gives `trl`
• ROT0 `c` gives `c`
• ROT26 `Cool` gives `Cool`
• ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.`
• ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.`

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.

### RotationalCipherTest.scala

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

/** @version 1.2.0 */
class RotationalCipherTest extends FunSuite with Matchers {

test("rotate a by 0, same output as input") {
RotationalCipher.rotate("a", 0) should be("a")
}

test("rotate a by 1") {
pending
RotationalCipher.rotate("a", 1) should be("b")
}

test("rotate a by 26, same output as input") {
pending
RotationalCipher.rotate("a", 26) should be("a")
}

test("rotate m by 13") {
pending
RotationalCipher.rotate("m", 13) should be("z")
}

test("rotate n by 13 with wrap around alphabet") {
pending
RotationalCipher.rotate("n", 13) should be("a")
}

test("rotate capital letters") {
pending
RotationalCipher.rotate("OMG", 5) should be("TRL")
}

test("rotate spaces") {
pending
RotationalCipher.rotate("O M G", 5) should be("T R L")
}

test("rotate numbers") {
pending
RotationalCipher.rotate("Testing 1 2 3 testing", 4) should be(
"Xiwxmrk 1 2 3 xiwxmrk")
}

test("rotate punctuation") {
pending
RotationalCipher.rotate("Let's eat, Grandma!", 21) should be(
"Gzo'n zvo, Bmviyhv!")
}

test("rotate all letters") {
pending
RotationalCipher.rotate("The quick brown fox jumps over the lazy dog.", 13) should be(
"Gur dhvpx oebja sbk whzcf bire gur ynml qbt.")
}
}``````
``````object RotationalCipher {
private val lowStart: Int = 'a'.toInt
private val lowEnd: Int = 'z'.toInt
private val upperStart: Int = 'A'.toInt
private val upperEnd: Int = 'Z'.toInt

def rotate(in: String, key: Int): String = in.map({
case x if upperStart <= x && x <= upperEnd => (((x - upperStart) + key) % 26 + upperStart).toChar
case x if lowStart <= x && x <= lowEnd => (((x - lowStart) + key) % 26 + lowStart).toChar
case x => x
}).mkString
}``````