Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Add rotational-cipher exercise (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored May 8, 2023
1 parent dccaa80 commit 6d3e232
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,17 @@
],
"difficulty": 2
},
{
"slug": "rotational-cipher",
"name": "Rotational Cipher",
"uuid": "af9aa984-1a9a-479b-94dc-8e7290ffa80e",
"practices": [],
"prerequisites": [
"numbers",
"strings"
],
"difficulty": 2
},
{
"slug": "atbash-cipher",
"name": "Atbash Cipher",
Expand Down
29 changes: 29 additions & 0 deletions exercises/practice/rotational-cipher/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Instructions

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:

```text
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.`
19 changes: 19 additions & 0 deletions exercises/practice/rotational-cipher/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"ErikSchierboom"
],
"files": {
"solution": [
"src/rotational_cipher.cljs"
],
"test": [
"test/rotational_cipher_test.cljs"
],
"example": [
".meta/src/example.cljs"
]
},
"blurb": "Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Caesar_cipher"
}
12 changes: 12 additions & 0 deletions exercises/practice/rotational-cipher/.meta/src/example.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; From https://exercism.org/tracks/clojure/exercises/rotational-cipher/solutions/acamargo

(ns rotational-cipher
(:require [clojure.string :as s]))

(def ^:const alphabet "abcdefghijklmnopqrstuvwxyz")

(defn rotate [text key]
(let [shifted (take 26 (drop (mod key 26) (cycle alphabet)))
cipher (zipmap (str alphabet (s/upper-case alphabet))
(concat shifted (map s/upper-case shifted)))]
(s/join (map #(cipher %1 %1) text))))
33 changes: 33 additions & 0 deletions exercises/practice/rotational-cipher/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.

[74e58a38-e484-43f1-9466-877a7515e10f]
description = "rotate a by 0, same output as input"

[7ee352c6-e6b0-4930-b903-d09943ecb8f5]
description = "rotate a by 1"

[edf0a733-4231-4594-a5ee-46a4009ad764]
description = "rotate a by 26, same output as input"

[e3e82cb9-2a5b-403f-9931-e43213879300]
description = "rotate m by 13"

[19f9eb78-e2ad-4da4-8fe3-9291d47c1709]
description = "rotate n by 13 with wrap around alphabet"

[a116aef4-225b-4da9-884f-e8023ca6408a]
description = "rotate capital letters"

[71b541bb-819c-4dc6-a9c3-132ef9bb737b]
description = "rotate spaces"

[ef32601d-e9ef-4b29-b2b5-8971392282e6]
description = "rotate numbers"

[32dd74f6-db2b-41a6-b02c-82eb4f93e549]
description = "rotate punctuation"

[9fb93fe6-42b0-46e6-9ec1-0bf0a062d8c9]
description = "rotate all letters"
10 changes: 10 additions & 0 deletions exercises/practice/rotational-cipher/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{:deps
{org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/clojurescript {:mvn/version "1.10.773"}}

:aliases
{:test
{:extra-paths ["test"]
:extra-deps
{olical/cljs-test-runner {:mvn/version "3.8.0"}}
:main-opts ["-m" "cljs-test-runner.main"]}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns rotational-cipher)

(defn rotate [] ;; <- arglist goes here
;; your code goes here
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(ns rotational-cipher-test
(:require [clojure.test :refer [deftest is testing]]
rotational-cipher))

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

(deftest rotate-a-by-26-same-output
(is (= (rotational-cipher/rotate "a" 26) "a")))

(deftest rotate-a-by-0-same-output
(is (= (rotational-cipher/rotate "a" 0) "a")))

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

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

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

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

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

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

(deftest rotate-opposite-direction
(is (= (rotational-cipher/rotate "b" -1) "a")))

(deftest rotate-opposite-past-first-letter
(is (= (rotational-cipher/rotate "B" -2) "Z")))

(deftest rotate-opposite-past-letter-count
(is (= (rotational-cipher/rotate "B" -28) "Z")))

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

(deftest 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.")))

0 comments on commit 6d3e232

Please sign in to comment.