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

vpes's solution

to Rotational Cipher in the Erlang Track

Published at Jan 06 2020 · 0 comments
Test suite


This exercise has changed since this solution was written.

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.

Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, rebar3 is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

$ rebar3 eunit


For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.


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.


%% Based on canonical data version 1.2.0
%% https://github.com/exercism/problem-specifications/raw/master/exercises/rotational-cipher/canonical-data.json
%% This file is automatically generated from the exercises canonical data.



'1_rotate_a_by_0_same_output_as_input_test'() ->
    ?assertEqual("a", rotational_cipher:encrypt("a", 0)),
    ?assertEqual("a", rotational_cipher:decrypt("a", 0)).

'2_rotate_a_by_1_test'() ->
    ?assertEqual("b", rotational_cipher:encrypt("a", 1)),
    ?assertEqual("a", rotational_cipher:decrypt("b", 1)).

'3_rotate_a_by_26_same_output_as_input_test'() ->
    ?assertEqual("a", rotational_cipher:encrypt("a", 26)),
    ?assertEqual("a", rotational_cipher:decrypt("a", 26)).

'4_rotate_m_by_13_test'() ->
    ?assertEqual("z", rotational_cipher:encrypt("m", 13)),
    ?assertEqual("m", rotational_cipher:decrypt("z", 13)).

'5_rotate_n_by_13_with_wrap_around_alphabet_test'() ->
    ?assertEqual("a", rotational_cipher:encrypt("n", 13)),
    ?assertEqual("n", rotational_cipher:decrypt("a", 13)).

'6_rotate_capital_letters_test'() ->
		 rotational_cipher:encrypt("OMG", 5)),
		 rotational_cipher:decrypt("TRL", 5)).

'7_rotate_spaces_test'() ->
    ?assertEqual("T R L",
		 rotational_cipher:encrypt("O M G", 5)),
    ?assertEqual("O M G",
		 rotational_cipher:decrypt("T R L", 5)).

'8_rotate_numbers_test'() ->
    ?assertEqual("Xiwxmrk 1 2 3 xiwxmrk",
		 rotational_cipher:encrypt("Testing 1 2 3 testing", 4)),
    ?assertEqual("Testing 1 2 3 testing",
		 rotational_cipher:decrypt("Xiwxmrk 1 2 3 xiwxmrk", 4)).

'9_rotate_punctuation_test'() ->
    ?assertEqual("Gzo'n zvo, Bmviyhv!",
		 rotational_cipher:encrypt("Let's eat, Grandma!", 21)),
    ?assertEqual("Let's eat, Grandma!",
		 rotational_cipher:decrypt("Gzo'n zvo, Bmviyhv!", 21)).

'10_rotate_all_letters_test'() ->
    ?assertEqual("Gur dhvpx oebja sbk whzcf bire gur ynml "
		 rotational_cipher:encrypt("The quick brown fox jumps over the lazy "
    ?assertEqual("The quick brown fox jumps over the lazy "
		 rotational_cipher:decrypt("Gur dhvpx oebja sbk whzcf bire gur ynml "

-export([decrypt/2, encrypt/2]).

-spec decrypt(string(),pos_integer()) -> string().
decrypt(String, Key) -> encrypt(String,-Key).

-spec encrypt(string(),integer()) -> string().
encrypt(String, Key) when Key > 26 -> encrypt(String,Key-26);
encrypt(String, Key) when Key < 0 -> encrypt(String,Key+26);
encrypt(String, Key) -> rot(String,Key).

-spec rot(string(),pos_integer()) -> string().
rot([], _) -> [];
rot([H|T], Key) when H >= $a andalso H =< $z -> [$a + ((H-$a+Key)rem 26)|rot(T, Key)];
rot([H|T], Key) when H >= $A andalso H =< $Z -> [$A + ((H-$A+Key)rem 26)|rot(T, Key)];
rot([H|T], Key) -> [H|encrypt(T, Key)].

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?