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

Commit dccaa80

Browse files
Add atbash-cipher exercise (#114)
1 parent 2413f7f commit dccaa80

File tree

8 files changed

+193
-0
lines changed

8 files changed

+193
-0
lines changed

config.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,18 @@
585585
],
586586
"difficulty": 2
587587
},
588+
{
589+
"slug": "atbash-cipher",
590+
"name": "Atbash Cipher",
591+
"uuid": "afb8e97c-6600-419c-a121-7cd3bcf870ee",
592+
"practices": [],
593+
"prerequisites": [
594+
"numbers",
595+
"conditionals",
596+
"strings"
597+
],
598+
"difficulty": 3
599+
},
588600
{
589601
"slug": "minesweeper",
590602
"name": "Minesweeper",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Instructions
2+
3+
Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.
4+
5+
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.
6+
The first letter is replaced with the last letter, the second with the second-last, and so on.
7+
8+
An Atbash cipher for the Latin alphabet would be as follows:
9+
10+
```text
11+
Plain: abcdefghijklmnopqrstuvwxyz
12+
Cipher: zyxwvutsrqponmlkjihgfedcba
13+
```
14+
15+
It is a very weak cipher because it only has one possible key, and it is a simple mono-alphabetic substitution cipher.
16+
However, this may not have been an issue in the cipher's time.
17+
18+
Ciphertext is written out in groups of fixed length, the traditional group size being 5 letters, leaving numbers unchanged, and punctuation is excluded.
19+
This is to make it harder to guess things based on word boundaries.
20+
All text will be encoded as lowercase letters.
21+
22+
## Examples
23+
24+
- Encoding `test` gives `gvhg`
25+
- Encoding `x123 yes` gives `c123b vh`
26+
- Decoding `gvhg` gives `test`
27+
- Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `thequickbrownfoxjumpsoverthelazydog`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"ErikSchierboom"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/atbash_cipher.cljs"
8+
],
9+
"test": [
10+
"test/atbash_cipher_test.cljs"
11+
],
12+
"example": [
13+
".meta/src/example.cljs"
14+
]
15+
},
16+
"blurb": "Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Atbash"
19+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; Courtesy from: https://exercism.org/tracks/clojure/exercises/atbash-cipher/solutions/joebutler2
2+
3+
(ns atbash-cipher
4+
(:require [clojure.string :as str]))
5+
6+
(def translation
7+
{
8+
"a" "z"
9+
"b" "y"
10+
"c" "x"
11+
"d" "w"
12+
"e" "v"
13+
"f" "u"
14+
"g" "t"
15+
"h" "s"
16+
"i" "r"
17+
"j" "q"
18+
"k" "p"
19+
"l" "o"
20+
"m" "n"
21+
"n" "m"
22+
"o" "l"
23+
"p" "k"
24+
"q" "j"
25+
"r" "i"
26+
"s" "h"
27+
"t" "g"
28+
"u" "f"
29+
"v" "e"
30+
"w" "d"
31+
"x" "c"
32+
"y" "b"
33+
"z" "a"
34+
})
35+
36+
(defn encode [input]
37+
(->> input
38+
(str/lower-case)
39+
(re-seq #"\w|\d")
40+
(map #(get translation % %))
41+
(partition-all 5)
42+
(map #(cons %2 %) (repeat " "))
43+
(flatten)
44+
(str/join)
45+
(str/trimr)))
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[2f47ebe1-eab9-4d6b-b3c6-627562a31c77]
6+
description = "encode yes"
7+
8+
[b4ffe781-ea81-4b74-b268-cc58ba21c739]
9+
description = "encode no"
10+
11+
[10e48927-24ab-4c4d-9d3f-3067724ace00]
12+
description = "encode OMG"
13+
14+
[d59b8bc3-509a-4a9a-834c-6f501b98750b]
15+
description = "encode spaces"
16+
17+
[31d44b11-81b7-4a94-8b43-4af6a2449429]
18+
description = "encode mindblowingly"
19+
20+
[d503361a-1433-48c0-aae0-d41b5baa33ff]
21+
description = "encode numbers"
22+
23+
[79c8a2d5-0772-42d4-b41b-531d0b5da926]
24+
description = "encode deep thought"
25+
26+
[9ca13d23-d32a-4967-a1fd-6100b8742bab]
27+
description = "encode all the letters"
28+
29+
[bb50e087-7fdf-48e7-9223-284fe7e69851]
30+
description = "decode exercism"
31+
32+
[ac021097-cd5d-4717-8907-b0814b9e292c]
33+
description = "decode a sentence"
34+
35+
[18729de3-de74-49b8-b68c-025eaf77f851]
36+
description = "decode numbers"
37+
38+
[0f30325f-f53b-415d-ad3e-a7a4f63de034]
39+
description = "decode all the letters"
40+
41+
[39640287-30c6-4c8c-9bac-9d613d1a5674]
42+
description = "decode with too many spaces"
43+
44+
[b34edf13-34c0-49b5-aa21-0768928000d5]
45+
description = "decode with no spaces"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{:deps
2+
{org.clojure/clojure {:mvn/version "1.10.1"}
3+
org.clojure/clojurescript {:mvn/version "1.10.773"}}
4+
5+
:aliases
6+
{:test
7+
{:extra-paths ["test"]
8+
:extra-deps
9+
{olical/cljs-test-runner {:mvn/version "3.8.0"}}
10+
:main-opts ["-m" "cljs-test-runner.main"]}}}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(ns atbash-cipher)
2+
3+
(defn encode [] ;; <- arglist goes here
4+
;; your code goes here
5+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
(ns atbash-cipher-test
2+
(:require [clojure.test :refer [deftest is]]
3+
atbash-cipher))
4+
5+
(deftest encode-no
6+
(is (= "ml" (atbash-cipher/encode "no"))))
7+
8+
(deftest encode-yes
9+
(is (= "bvh" (atbash-cipher/encode "yes"))))
10+
11+
(deftest encode-OMG
12+
(is (= "lnt" (atbash-cipher/encode "OMG"))))
13+
14+
(deftest encode-O-M-G
15+
(is (= "lnt" (atbash-cipher/encode "O M G"))))
16+
17+
(deftest encode-long-word
18+
(is (= "nrmwy oldrm tob" (atbash-cipher/encode "mindblowingly"))))
19+
20+
(deftest encode-numbers
21+
(is (= "gvhgr mt123 gvhgr mt"
22+
(atbash-cipher/encode "Testing, 1 2 3, testing."))))
23+
24+
(deftest encode-sentence
25+
(is (= "gifgs rhurx grlm" (atbash-cipher/encode "Truth is fiction."))))
26+
27+
(deftest encode-all-the-things
28+
(let [plaintext "The quick brown fox jumps over the lazy dog."
29+
cipher "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"]
30+
(is (= cipher (atbash-cipher/encode plaintext)))))

0 commit comments

Comments
 (0)