Avatar of joebutler2

joebutler2's solution

to Rotational Cipher in the Clojure Track

Published at Jul 10 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.

Source

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.

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- normalize-offset
  "Takes an offset and constrains it to a range of -25 to 25."
  ; See if we can agjust this to 27 so we don't have to subtract 1 later on
  [offset]
  (if (pos? offset) (mod offset 26) (mod offset -26)))


(defn- is-upper-case? [char-int]
  (= Character/UPPERCASE_LETTER (Character/getType (char char-int))))


(defn- is-lower-case? [char-int]
  (= Character/LOWERCASE_LETTER (Character/getType (char char-int))))


(defn- constrain-uppercase [char-int]
  (let [lower-bound 65
        upper-bound 90]
  (cond (> char-int upper-bound)
        (dec (+ lower-bound (- char-int upper-bound)))
        (< char-int lower-bound)
        (dec (+ upper-bound (- lower-bound char-int)))
        :else char-int)))


(defn- constrain-lowercase [char-int]
  (let [lower-bound 97
        upper-bound 122]
    (cond
      (> char-int upper-bound) (dec (+ lower-bound (- char-int upper-bound)))
      (< char-int lower-bound) (dec (+ upper-bound (- lower-bound char-int)))
      :else char-int)))


(defn- char-int->character [char-int offset]
  (let [normalized-offset (normalize-offset offset)
        new-char-int (+ char-int normalized-offset)]
    (char (cond
      (is-upper-case? char-int) (constrain-uppercase new-char-int)
      (is-lower-case? char-int) (constrain-lowercase new-char-int)
      :else new-char-int))))


(defn rotate [text offset]
  (reduce
    (fn [acc char-int]
      (str acc (if (int? char-int)
                 (char-int->character char-int offset)
                 char-int)))
    ""
    (map #(if (Character/isAlphabetic (int %)) (int %) %) text)))

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?