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

Commit

Permalink
Add atbash-cipher exercise (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored May 7, 2023
1 parent 2413f7f commit dccaa80
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 0 deletions.
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,18 @@
],
"difficulty": 2
},
{
"slug": "atbash-cipher",
"name": "Atbash Cipher",
"uuid": "afb8e97c-6600-419c-a121-7cd3bcf870ee",
"practices": [],
"prerequisites": [
"numbers",
"conditionals",
"strings"
],
"difficulty": 3
},
{
"slug": "minesweeper",
"name": "Minesweeper",
Expand Down
27 changes: 27 additions & 0 deletions exercises/practice/atbash-cipher/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Instructions

Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.

The Atbash cipher is a simple substitution cipher that relies on transposing all the letters in the alphabet such that the resulting alphabet is backwards.
The first letter is replaced with the last letter, the second with the second-last, and so on.

An Atbash cipher for the Latin alphabet would be as follows:

```text
Plain: abcdefghijklmnopqrstuvwxyz
Cipher: zyxwvutsrqponmlkjihgfedcba
```

It is a very weak cipher because it only has one possible key, and it is a simple mono-alphabetic substitution cipher.
However, this may not have been an issue in the cipher's time.

Ciphertext is written out in groups of fixed length, the traditional group size being 5 letters, leaving numbers unchanged, and punctuation is excluded.
This is to make it harder to guess things based on word boundaries.
All text will be encoded as lowercase letters.

## Examples

- Encoding `test` gives `gvhg`
- Encoding `x123 yes` gives `c123b vh`
- Decoding `gvhg` gives `test`
- Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `thequickbrownfoxjumpsoverthelazydog`
19 changes: 19 additions & 0 deletions exercises/practice/atbash-cipher/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"ErikSchierboom"
],
"files": {
"solution": [
"src/atbash_cipher.cljs"
],
"test": [
"test/atbash_cipher_test.cljs"
],
"example": [
".meta/src/example.cljs"
]
},
"blurb": "Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Atbash"
}
45 changes: 45 additions & 0 deletions exercises/practice/atbash-cipher/.meta/src/example.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
; Courtesy from: https://exercism.org/tracks/clojure/exercises/atbash-cipher/solutions/joebutler2

(ns atbash-cipher
(:require [clojure.string :as str]))

(def translation
{
"a" "z"
"b" "y"
"c" "x"
"d" "w"
"e" "v"
"f" "u"
"g" "t"
"h" "s"
"i" "r"
"j" "q"
"k" "p"
"l" "o"
"m" "n"
"n" "m"
"o" "l"
"p" "k"
"q" "j"
"r" "i"
"s" "h"
"t" "g"
"u" "f"
"v" "e"
"w" "d"
"x" "c"
"y" "b"
"z" "a"
})

(defn encode [input]
(->> input
(str/lower-case)
(re-seq #"\w|\d")
(map #(get translation % %))
(partition-all 5)
(map #(cons %2 %) (repeat " "))
(flatten)
(str/join)
(str/trimr)))
45 changes: 45 additions & 0 deletions exercises/practice/atbash-cipher/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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.

[2f47ebe1-eab9-4d6b-b3c6-627562a31c77]
description = "encode yes"

[b4ffe781-ea81-4b74-b268-cc58ba21c739]
description = "encode no"

[10e48927-24ab-4c4d-9d3f-3067724ace00]
description = "encode OMG"

[d59b8bc3-509a-4a9a-834c-6f501b98750b]
description = "encode spaces"

[31d44b11-81b7-4a94-8b43-4af6a2449429]
description = "encode mindblowingly"

[d503361a-1433-48c0-aae0-d41b5baa33ff]
description = "encode numbers"

[79c8a2d5-0772-42d4-b41b-531d0b5da926]
description = "encode deep thought"

[9ca13d23-d32a-4967-a1fd-6100b8742bab]
description = "encode all the letters"

[bb50e087-7fdf-48e7-9223-284fe7e69851]
description = "decode exercism"

[ac021097-cd5d-4717-8907-b0814b9e292c]
description = "decode a sentence"

[18729de3-de74-49b8-b68c-025eaf77f851]
description = "decode numbers"

[0f30325f-f53b-415d-ad3e-a7a4f63de034]
description = "decode all the letters"

[39640287-30c6-4c8c-9bac-9d613d1a5674]
description = "decode with too many spaces"

[b34edf13-34c0-49b5-aa21-0768928000d5]
description = "decode with no spaces"
10 changes: 10 additions & 0 deletions exercises/practice/atbash-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"]}}}
5 changes: 5 additions & 0 deletions exercises/practice/atbash-cipher/src/atbash_cipher.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns atbash-cipher)

(defn encode [] ;; <- arglist goes here
;; your code goes here
)
30 changes: 30 additions & 0 deletions exercises/practice/atbash-cipher/test/atbash_cipher_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(ns atbash-cipher-test
(:require [clojure.test :refer [deftest is]]
atbash-cipher))

(deftest encode-no
(is (= "ml" (atbash-cipher/encode "no"))))

(deftest encode-yes
(is (= "bvh" (atbash-cipher/encode "yes"))))

(deftest encode-OMG
(is (= "lnt" (atbash-cipher/encode "OMG"))))

(deftest encode-O-M-G
(is (= "lnt" (atbash-cipher/encode "O M G"))))

(deftest encode-long-word
(is (= "nrmwy oldrm tob" (atbash-cipher/encode "mindblowingly"))))

(deftest encode-numbers
(is (= "gvhgr mt123 gvhgr mt"
(atbash-cipher/encode "Testing, 1 2 3, testing."))))

(deftest encode-sentence
(is (= "gifgs rhurx grlm" (atbash-cipher/encode "Truth is fiction."))))

(deftest encode-all-the-things
(let [plaintext "The quick brown fox jumps over the lazy dog."
cipher "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"]
(is (= cipher (atbash-cipher/encode plaintext)))))

0 comments on commit dccaa80

Please sign in to comment.