Skip to content

Commit b181b25

Browse files
authored
Add atbash-cipher (#78)
* add `atbash-cipher` * encode and then decode
1 parent 68bdc16 commit b181b25

9 files changed

Lines changed: 217 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
"prerequisites": [],
5151
"difficulty": 2
5252
},
53+
{
54+
"slug": "atbash-cipher",
55+
"name": "Atbash Cipher",
56+
"uuid": "dc112a08-5969-490e-8108-833f6f927cb2",
57+
"practices": [],
58+
"prerequisites": [],
59+
"difficulty": 2
60+
},
5361
{
5462
"slug": "collatz-conjecture",
5563
"name": "Collatz Conjecture",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
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+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"atbash_cipher.moon"
8+
],
9+
"test": [
10+
"atbash_cipher_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
encode: (phrase) ->
3+
clean = phrase\lower!\gsub "[^a-z0-9]", ""
4+
decoded = clean\gsub "[a-z]", (letter) ->
5+
string.char 219 - letter\byte!
6+
spaced = decoded\gsub(".....", "%0 ")
7+
spaced\match("^%s*(.-)%s*$") or spaced
8+
9+
decode: (phrase) ->
10+
clean = phrase\lower!\gsub "[^a-z0-9]", ""
11+
clean\gsub "[a-z]", (letter) ->
12+
string.char 219 - letter\byte!
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
module_name: 'AtbashCipher',
3+
4+
generate_test: (case, level) ->
5+
lines = {
6+
"result = AtbashCipher.#{case.property} #{quote case.input.phrase}"
7+
"expected = #{quote case.expected}"
8+
"assert.are.equal expected, result"
9+
}
10+
11+
table.concat [indent line, level for line in *lines], '\n'
12+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[2f47ebe1-eab9-4d6b-b3c6-627562a31c77]
13+
description = "encode -> encode yes"
14+
15+
[b4ffe781-ea81-4b74-b268-cc58ba21c739]
16+
description = "encode -> encode no"
17+
18+
[10e48927-24ab-4c4d-9d3f-3067724ace00]
19+
description = "encode -> encode OMG"
20+
21+
[d59b8bc3-509a-4a9a-834c-6f501b98750b]
22+
description = "encode -> encode spaces"
23+
24+
[31d44b11-81b7-4a94-8b43-4af6a2449429]
25+
description = "encode -> encode mindblowingly"
26+
27+
[d503361a-1433-48c0-aae0-d41b5baa33ff]
28+
description = "encode -> encode numbers"
29+
30+
[79c8a2d5-0772-42d4-b41b-531d0b5da926]
31+
description = "encode -> encode deep thought"
32+
33+
[9ca13d23-d32a-4967-a1fd-6100b8742bab]
34+
description = "encode -> encode all the letters"
35+
36+
[bb50e087-7fdf-48e7-9223-284fe7e69851]
37+
description = "decode -> decode exercism"
38+
39+
[ac021097-cd5d-4717-8907-b0814b9e292c]
40+
description = "decode -> decode a sentence"
41+
42+
[18729de3-de74-49b8-b68c-025eaf77f851]
43+
description = "decode -> decode numbers"
44+
45+
[0f30325f-f53b-415d-ad3e-a7a4f63de034]
46+
description = "decode -> decode all the letters"
47+
48+
[39640287-30c6-4c8c-9bac-9d613d1a5674]
49+
description = "decode -> decode with too many spaces"
50+
51+
[b34edf13-34c0-49b5-aa21-0768928000d5]
52+
description = "decode -> decode with no spaces"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
encode: (phrase) ->
3+
error 'Implement me'
4+
5+
decode: (phrase) ->
6+
error 'Implement me'
7+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
AtbashCipher = require 'atbash_cipher'
2+
3+
describe 'atbash-cipher', ->
4+
describe 'encode', ->
5+
it 'encode yes', ->
6+
result = AtbashCipher.encode 'yes'
7+
expected = 'bvh'
8+
assert.are.equal expected, result
9+
10+
pending 'encode no', ->
11+
result = AtbashCipher.encode 'no'
12+
expected = 'ml'
13+
assert.are.equal expected, result
14+
15+
pending 'encode OMG', ->
16+
result = AtbashCipher.encode 'OMG'
17+
expected = 'lnt'
18+
assert.are.equal expected, result
19+
20+
pending 'encode spaces', ->
21+
result = AtbashCipher.encode 'O M G'
22+
expected = 'lnt'
23+
assert.are.equal expected, result
24+
25+
pending 'encode mindblowingly', ->
26+
result = AtbashCipher.encode 'mindblowingly'
27+
expected = 'nrmwy oldrm tob'
28+
assert.are.equal expected, result
29+
30+
pending 'encode numbers', ->
31+
result = AtbashCipher.encode 'Testing,1 2 3, testing.'
32+
expected = 'gvhgr mt123 gvhgr mt'
33+
assert.are.equal expected, result
34+
35+
pending 'encode deep thought', ->
36+
result = AtbashCipher.encode 'Truth is fiction.'
37+
expected = 'gifgs rhurx grlm'
38+
assert.are.equal expected, result
39+
40+
pending 'encode all the letters', ->
41+
result = AtbashCipher.encode 'The quick brown fox jumps over the lazy dog.'
42+
expected = 'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt'
43+
assert.are.equal expected, result
44+
45+
describe 'decode', ->
46+
pending 'decode exercism', ->
47+
result = AtbashCipher.decode 'vcvix rhn'
48+
expected = 'exercism'
49+
assert.are.equal expected, result
50+
51+
pending 'decode a sentence', ->
52+
result = AtbashCipher.decode 'zmlyh gzxov rhlug vmzhg vkkrm thglm v'
53+
expected = 'anobstacleisoftenasteppingstone'
54+
assert.are.equal expected, result
55+
56+
pending 'decode numbers', ->
57+
result = AtbashCipher.decode 'gvhgr mt123 gvhgr mt'
58+
expected = 'testing123testing'
59+
assert.are.equal expected, result
60+
61+
pending 'decode all the letters', ->
62+
result = AtbashCipher.decode 'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt'
63+
expected = 'thequickbrownfoxjumpsoverthelazydog'
64+
assert.are.equal expected, result
65+
66+
pending 'decode with too many spaces', ->
67+
result = AtbashCipher.decode 'vc vix r hn'
68+
expected = 'exercism'
69+
assert.are.equal expected, result
70+
71+
pending 'decode with no spaces', ->
72+
result = AtbashCipher.decode 'zmlyhgzxovrhlugvmzhgvkkrmthglmv'
73+
expected = 'anobstacleisoftenasteppingstone'
74+
assert.are.equal expected, result

0 commit comments

Comments
 (0)