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

EmilienMottet's solution

to Rotational Cipher in the Erlang Track

Published at Nov 27 2020 · 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.`

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

``````%% Generated with 'testgen v0.2.0'
%% Revision 1 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/42dd0cea20498fd544b152c4e2c0a419bb7e266a/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_'() ->
[{"rotate a by 0, same output as input "
"(encrypt)",
?_assertEqual("a", rotational_cipher:encrypt("a", 0))},
{"rotate a by 0, same output as input "
"(decrypt)",
?_assertEqual("a", rotational_cipher:decrypt("a", 0))}].

'2_rotate_a_by_1_test_'() ->
[{"rotate a by 1 (encrypt)",
?_assertEqual("b", rotational_cipher:encrypt("a", 1))},
{"rotate a by 1 (decrypt)",
?_assertEqual("a", rotational_cipher:decrypt("b", 1))}].

'3_rotate_a_by_26_same_output_as_input_test_'() ->
[{"rotate a by 26, same output as input "
"(encrypt)",
?_assertEqual("a", rotational_cipher:encrypt("a", 26))},
{"rotate a by 26, same output as input "
"(decrypt)",
?_assertEqual("a",
rotational_cipher:decrypt("a", 26))}].

'4_rotate_m_by_13_test_'() ->
[{"rotate m by 13 (encrypt)",
?_assertEqual("z", rotational_cipher:encrypt("m", 13))},
{"rotate m by 13 (decrypt)",
?_assertEqual("m",
rotational_cipher:decrypt("z", 13))}].

'5_rotate_n_by_13_with_wrap_around_alphabet_test_'() ->
[{"rotate n by 13 with wrap around alphabet "
"(encrypt)",
?_assertEqual("a", rotational_cipher:encrypt("n", 13))},
{"rotate n by 13 with wrap around alphabet "
"(decrypt)",
?_assertEqual("n",
rotational_cipher:decrypt("a", 13))}].

'6_rotate_capital_letters_test_'() ->
[{"rotate capital letters (encrypt)",
?_assertEqual("TRL",
rotational_cipher:encrypt("OMG", 5))},
{"rotate capital letters (decrypt)",
?_assertEqual("OMG",
rotational_cipher:decrypt("TRL", 5))}].

'7_rotate_spaces_test_'() ->
[{"rotate spaces (encrypt)",
?_assertEqual("T R L",
rotational_cipher:encrypt("O M G", 5))},
{"rotate spaces (decrypt)",
?_assertEqual("O M G",
rotational_cipher:decrypt("T R L", 5))}].

'8_rotate_numbers_test_'() ->
[{"rotate numbers (encrypt)",
?_assertEqual("Xiwxmrk 1 2 3 xiwxmrk",
rotational_cipher:encrypt("Testing 1 2 3 testing", 4))},
{"rotate numbers (decrypt)",
?_assertEqual("Testing 1 2 3 testing",
rotational_cipher:decrypt("Xiwxmrk 1 2 3 xiwxmrk",
4))}].

'9_rotate_punctuation_test_'() ->
[{"rotate punctuation (encrypt)",
?_assertEqual("Gzo'n zvo, Bmviyhv!",
rotational_cipher:encrypt("Let's eat, Grandma!", 21))},
{"rotate punctuation (decrypt)",
?_assertEqual("Let's eat, Grandma!",
rotational_cipher:decrypt("Gzo'n zvo, Bmviyhv!", 21))}].

'10_rotate_all_letters_test_'() ->
[{"rotate all letters (encrypt)",
?_assertEqual("Gur dhvpx oebja sbk whzcf bire gur ynml "
"qbt.",
rotational_cipher:encrypt("The quick brown fox jumps over the lazy "
"dog.",
13))},
{"rotate all letters (decrypt)",
?_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) ->
[decrypt_char(C, Key) || C <- String].

encrypt(String, Key) ->
[encrypt_char(C, Key) || C <- String].

decrypt_char(C, Key) when (C >= \$a) and (C =< \$z) ->
(\$z - \$a + 1 + C - \$a - Key) rem (\$z - \$a + 1) + \$a;
decrypt_char(C, Key) when (C >= \$A) and (C =< \$Z) ->
(\$Z - \$A + 1 + C - \$A - Key) rem (\$Z - \$A + 1) + \$A;
decrypt_char(C, _Key) ->
C.

encrypt_char(C, Key) when (C >= \$a) and (C =< \$z) ->
(\$z - \$a + 1 + C - \$a + Key) rem (\$z - \$a + 1) + \$a;
encrypt_char(C, Key) when (C >= \$A) and (C =< \$Z) ->
(\$Z - \$A + 1 + C - \$A + Key) rem (\$Z - \$A + 1) + \$A;
encrypt_char(C, _Key) ->
C.``````