Skip to content

Commit 6795f59

Browse files
authored
Add atbash-cipher (#190)
1 parent 932580c commit 6795f59

File tree

8 files changed

+538
-0
lines changed

8 files changed

+538
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@
5858
"prerequisites": [],
5959
"difficulty": 2
6060
},
61+
{
62+
"slug": "atbash-cipher",
63+
"name": "Atbash Cipher",
64+
"uuid": "e63adab3-3824-499f-b6d7-e052fb04844c",
65+
"practices": [],
66+
"prerequisites": [],
67+
"difficulty": 2
68+
},
6169
{
6270
"slug": "binary-search",
6371
"name": "Binary Search",
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.red"
8+
],
9+
"test": [
10+
"atbash-cipher-test.red"
11+
],
12+
"example": [
13+
".meta/example.red"
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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Red [
2+
description: {"Atbash Cipher" exercise solution for exercism platform}
3+
author: "BNAndras"
4+
]
5+
6+
encode: function [
7+
phrase
8+
] [
9+
count: 0
10+
result: copy ""
11+
12+
foreach char lowercase phrase [
13+
if or~ whitespace? char punctuation? char [continue]
14+
15+
16+
if count = 5 [
17+
append result #" "
18+
count: 0
19+
]
20+
21+
encoded: char
22+
if and~ char >= #"a" char <= #"z" [
23+
encoded: to-char (#"z" - char + #"a")
24+
]
25+
26+
append result encoded
27+
28+
count: count + 1
29+
]
30+
31+
result
32+
]
33+
34+
decode: function [
35+
phrase
36+
] [
37+
count: 0
38+
result: copy ""
39+
40+
foreach char phrase [
41+
if and~ char >= #"a" char <= #"z" [
42+
append result to-char (#"z" - char + #"a")
43+
]
44+
if and~ char >= #"0" char <= #"9" [
45+
append result char
46+
]
47+
]
48+
49+
result
50+
]
51+
52+
whitespace?: function [
53+
char
54+
] [
55+
parse to-string char [any [" " | tab | newline]]
56+
]
57+
58+
punctuation?: function [
59+
char
60+
] [
61+
parse to-string char [any [#"." | "," | "!" | "?" | "'" ]]
62+
]
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: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
Red [
2+
description: {Tests for "Atbash Cipher" Exercism exercise}
3+
author: "loziniak"
4+
]
5+
6+
#include %testlib.red
7+
8+
test-init/limit %atbash-cipher.red 1
9+
; test-init/limit %.meta/example.red 1 ; test example solution
10+
11+
canonical-cases: [#[
12+
description: "encode yes"
13+
input: #[
14+
phrase: "yes"
15+
]
16+
expected: "bvh"
17+
function: "encode"
18+
uuid: "2f47ebe1-eab9-4d6b-b3c6-627562a31c77"
19+
] #[
20+
description: "encode no"
21+
input: #[
22+
phrase: "no"
23+
]
24+
expected: "ml"
25+
function: "encode"
26+
uuid: "b4ffe781-ea81-4b74-b268-cc58ba21c739"
27+
] #[
28+
description: "encode OMG"
29+
input: #[
30+
phrase: "OMG"
31+
]
32+
expected: "lnt"
33+
function: "encode"
34+
uuid: "10e48927-24ab-4c4d-9d3f-3067724ace00"
35+
] #[
36+
description: "encode spaces"
37+
input: #[
38+
phrase: "O M G"
39+
]
40+
expected: "lnt"
41+
function: "encode"
42+
uuid: "d59b8bc3-509a-4a9a-834c-6f501b98750b"
43+
] #[
44+
description: "encode mindblowingly"
45+
input: #[
46+
phrase: "mindblowingly"
47+
]
48+
expected: "nrmwy oldrm tob"
49+
function: "encode"
50+
uuid: "31d44b11-81b7-4a94-8b43-4af6a2449429"
51+
] #[
52+
description: "encode numbers"
53+
input: #[
54+
phrase: "Testing,1 2 3, testing."
55+
]
56+
expected: "gvhgr mt123 gvhgr mt"
57+
function: "encode"
58+
uuid: "d503361a-1433-48c0-aae0-d41b5baa33ff"
59+
] #[
60+
description: "encode deep thought"
61+
input: #[
62+
phrase: "Truth is fiction."
63+
]
64+
expected: "gifgs rhurx grlm"
65+
function: "encode"
66+
uuid: "79c8a2d5-0772-42d4-b41b-531d0b5da926"
67+
] #[
68+
description: "encode all the letters"
69+
input: #[
70+
phrase: "The quick brown fox jumps over the lazy dog."
71+
]
72+
expected: "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
73+
function: "encode"
74+
uuid: "9ca13d23-d32a-4967-a1fd-6100b8742bab"
75+
] #[
76+
description: "decode exercism"
77+
input: #[
78+
phrase: "vcvix rhn"
79+
]
80+
expected: "exercism"
81+
function: "decode"
82+
uuid: "bb50e087-7fdf-48e7-9223-284fe7e69851"
83+
] #[
84+
description: "decode a sentence"
85+
input: #[
86+
phrase: "zmlyh gzxov rhlug vmzhg vkkrm thglm v"
87+
]
88+
expected: "anobstacleisoftenasteppingstone"
89+
function: "decode"
90+
uuid: "ac021097-cd5d-4717-8907-b0814b9e292c"
91+
] #[
92+
description: "decode numbers"
93+
input: #[
94+
phrase: "gvhgr mt123 gvhgr mt"
95+
]
96+
expected: "testing123testing"
97+
function: "decode"
98+
uuid: "18729de3-de74-49b8-b68c-025eaf77f851"
99+
] #[
100+
description: "decode all the letters"
101+
input: #[
102+
phrase: "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
103+
]
104+
expected: "thequickbrownfoxjumpsoverthelazydog"
105+
function: "decode"
106+
uuid: "0f30325f-f53b-415d-ad3e-a7a4f63de034"
107+
] #[
108+
description: "decode with too many spaces"
109+
input: #[
110+
phrase: "vc vix r hn"
111+
]
112+
expected: "exercism"
113+
function: "decode"
114+
uuid: "39640287-30c6-4c8c-9bac-9d613d1a5674"
115+
] #[
116+
description: "decode with no spaces"
117+
input: #[
118+
phrase: "zmlyhgzxovrhlugvmzhgvkkrmthglmv"
119+
]
120+
expected: "anobstacleisoftenasteppingstone"
121+
function: "decode"
122+
uuid: "b34edf13-34c0-49b5-aa21-0768928000d5"
123+
]]
124+
125+
126+
foreach c-case canonical-cases [
127+
case-code: reduce [
128+
'expect c-case/expected compose [
129+
(to word! c-case/function) (values-of c-case/input)
130+
]
131+
]
132+
133+
test c-case/description case-code
134+
]
135+
136+
test-results/print
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Red [
2+
description: {"Atbash Cipher" exercise solution for exercism platform}
3+
author: "" ; you can write your name here, in quotes
4+
]
5+
6+
encode: function [
7+
phrase
8+
] [
9+
cause-error 'user 'message "You need to implement encode function."
10+
]
11+
12+
decode: function [
13+
phrase
14+
] [
15+
cause-error 'user 'message "You need to implement decode function."
16+
]
17+

0 commit comments

Comments
 (0)