# r36's solution

## to Rotational Cipher in the Erlang Track

Published at Aug 27 2020 · 0 comments
Instructions
Test suite
Solution

#### Note:

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.

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

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

## Questions?

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.

## Submitting Incomplete Solutions

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

### rotational_cipher_tests.erl

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

-module(rotational_cipher_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

'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'() ->
?assertEqual("TRL",
rotational_cipher:encrypt("OMG", 5)),
?assertEqual("OMG",
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 "
"qbt.",
rotational_cipher:encrypt("The quick brown fox jumps over the lazy "
"dog.",
13)),
?assertEqual("The quick brown fox jumps over the lazy "
"dog.",
rotational_cipher:decrypt("Gur dhvpx oebja sbk whzcf bire gur ynml "
"qbt.",
13)).``````
``````-module(rotational_cipher).

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

decrypt(String, Key) -> rot(String,-Key,[]).

encrypt(String, Key) -> rot(String,Key,[]).

rot([],_Key,Acc)->lists:reverse(Acc);
rot([H|T],Key,Acc)->
HNew = case  one_chr(H) of
undefined -> H;
A ->    A+case (H-A+Key)rem 26 of
C when C<0 -> 26-abs(C);
C->C
end
end,
rot(T,Key,[HNew|Acc]).

one_chr(H) when H>=\$A andalso H=<\$Z ->\$A;
one_chr(H) when H>=\$a andalso H=<\$z ->\$a;
one_chr(_)->undefined.``````