# lluca2's solution

## to Rotational Cipher in the Clojure Track

Published at Aug 19 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.`

## Submitting Incomplete Solutions

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

### rotational_cipher_test.clj

``````(ns rotational-cipher-test
(:require  [clojure.test :refer [deftest is testing]]
rotational-cipher))

(deftest rotational-cipher-test
(testing "rotate a by 1"
(is (= (rotational-cipher/rotate "a" 1) "b")))

(testing "rotate a by 26, same output as input"
(is (= (rotational-cipher/rotate "a" 26) "a")))

(testing "rotate a by 0, same output as input"
(is (= (rotational-cipher/rotate "a" 0) "a")))

(testing "rotate m by 13"
(is (= (rotational-cipher/rotate "m" 13) "z")))

(testing "rotate n by 13 with wrap around alphabet"
(is (= (rotational-cipher/rotate "n" 13) "a")))

(testing "rotate capital letters"
(is (= (rotational-cipher/rotate "OMG" 5) "TRL")))

(testing "rotate spaces"
(is (= (rotational-cipher/rotate "O M G" 5) "T R L")))

(testing "rotate numbers"
(is (= (rotational-cipher/rotate "Testing 1 2 3 testing" 4) "Xiwxmrk 1 2 3 xiwxmrk")))

(testing "rotate punctuation"
(is (= (rotational-cipher/rotate "Let's eat, Grandma!" 21) "Gzo'n zvo, Bmviyhv!")))

(testing "rotate in the opposite direction"
(is (= (rotational-cipher/rotate "b" -1) "a")))

(testing "rotate in the opposite direction past first letter"
(is (= (rotational-cipher/rotate "B" -2) "Z")))

(testing "rotate in the opposite direction past letter count"
(is (= (rotational-cipher/rotate "B" -28) "Z")))

(testing "rotate forward then backwards the same number of steps"
(is (=  (rotational-cipher/rotate
(rotational-cipher/rotate "B" 28) -28) "B")))

(testing "rotate all letters"
(is (= (rotational-cipher/rotate "The quick brown fox jumps over the lazy dog." 13) "Gur dhvpx oebja sbk whzcf bire gur ynml qbt."))))``````
``````(ns rotational-cipher)

(defn encode-char [c offset]
(let [char-code (int c)]
(cond
(Character/isLowerCase char-code) (char (+ 97 (mod (+ -97 char-code offset) 26)))
(Character/isUpperCase char-code) (char (+ 65 (mod (+ -65 char-code offset) 26)))
:else c)))

(defn rotate [plaintext offset]
(->> plaintext
(map #(encode-char % offset))
(apply str)))``````