🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of SergiiVlasiuk

SergiiVlasiuk's solution

to Rotational Cipher in the Scala Track

Published at Sep 01 2019 · 0 comments
Test suite

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.


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


Wikipedia https://en.wikipedia.org/wiki/Caesar_cipher

Submitting Incomplete Solutions

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


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") {
    RotationalCipher.rotate("a", 1) should be("b")

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

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

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

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

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

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

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

  test("rotate all letters") {
    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

Community comments

Find this solution interesting? Ask the author a question to learn more.

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?
  • Are there new concepts here that you could read more about to improve your understanding?