Skip to content

Commit

Permalink
Add atbash-cipher (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
ageron authored Oct 11, 2024
1 parent 27df90f commit 62abc6d
Show file tree
Hide file tree
Showing 8 changed files with 288 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "atbash-cipher",
"name": "Atbash Cipher",
"uuid": "57e5a257-9afd-46f1-8c43-6fbd6f371ca3",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "binary-search",
"name": "Binary Search",
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`
34 changes: 34 additions & 0 deletions exercises/practice/atbash-cipher/.meta/Example.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module [encode, decode]

invert : U8 -> U8
invert = \char ->
if char >= 'a' && char <= 'z' then
'z' - char + 'a'
else
char

encode : Str -> Result Str _
encode = \phrase ->
phrase
|> Str.toUtf8
|> List.joinMap \char ->
if char >= 'A' && char <= 'Z' then
[invert (char - 'A' + 'a')]
else if char >= 'a' && char <= 'z' then
[invert char]
else if char >= '0' && char <= '9' then
[char]
else
[]
|> List.chunksOf 5
|> List.intersperse [' ']
|> List.join
|> Str.fromUtf8

decode : Str -> Result Str _
decode = \phrase ->
phrase
|> Str.toUtf8
|> List.dropIf \c -> c == ' '
|> List.map invert
|> Str.fromUtf8
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": [
"ageron"
],
"files": {
"solution": [
"AtbashCipher.roc"
],
"test": [
"atbash-cipher-test.roc"
],
"example": [
".meta/Example.roc"
]
},
"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"
}
21 changes: 21 additions & 0 deletions exercises/practice/atbash-cipher/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{%- import "generator_macros.j2" as macros with context -%}
{{ macros.canonical_ref() }}
{{ macros.header() }}

import {{ exercise | to_pascal }} exposing [encode, decode]

{% for supercase in cases %}
##
## {{ supercase["description"] }}
##

{% for case in supercase["cases"] -%}
# {{ case["description"] }}
expect
phrase = {{ case["input"]["phrase"] | to_roc }}
result = phrase |> {{ case["property"] | to_camel }}
expected = {{ case["expected"] | to_roc }}
result == Ok expected

{% endfor %}
{% endfor %}
52 changes: 52 additions & 0 deletions exercises/practice/atbash-cipher/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

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

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

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

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

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

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

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

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

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

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

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

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

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

[b34edf13-34c0-49b5-aa21-0768928000d5]
description = "decode -> decode with no spaces"
9 changes: 9 additions & 0 deletions exercises/practice/atbash-cipher/AtbashCipher.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module [encode, decode]

encode : Str -> Result Str _
encode = \phrase ->
crash "Please implement the 'encode' function"

decode : Str -> Result Str _
decode = \phrase ->
crash "Please implement the 'decode' function"
118 changes: 118 additions & 0 deletions exercises/practice/atbash-cipher/atbash-cipher-test.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# These tests are auto-generated with test data from:
# https://github.com/exercism/problem-specifications/tree/main/exercises/atbash-cipher/canonical-data.json
# File last updated on 2024-10-10
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
}

main =
Task.ok {}

import AtbashCipher exposing [encode, decode]

##
## encode
##

# encode yes
expect
phrase = "yes"
result = phrase |> encode
expected = "bvh"
result == Ok expected

# encode no
expect
phrase = "no"
result = phrase |> encode
expected = "ml"
result == Ok expected

# encode OMG
expect
phrase = "OMG"
result = phrase |> encode
expected = "lnt"
result == Ok expected

# encode spaces
expect
phrase = "O M G"
result = phrase |> encode
expected = "lnt"
result == Ok expected

# encode mindblowingly
expect
phrase = "mindblowingly"
result = phrase |> encode
expected = "nrmwy oldrm tob"
result == Ok expected

# encode numbers
expect
phrase = "Testing,1 2 3, testing."
result = phrase |> encode
expected = "gvhgr mt123 gvhgr mt"
result == Ok expected

# encode deep thought
expect
phrase = "Truth is fiction."
result = phrase |> encode
expected = "gifgs rhurx grlm"
result == Ok expected

# encode all the letters
expect
phrase = "The quick brown fox jumps over the lazy dog."
result = phrase |> encode
expected = "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
result == Ok expected

##
## decode
##

# decode exercism
expect
phrase = "vcvix rhn"
result = phrase |> decode
expected = "exercism"
result == Ok expected

# decode a sentence
expect
phrase = "zmlyh gzxov rhlug vmzhg vkkrm thglm v"
result = phrase |> decode
expected = "anobstacleisoftenasteppingstone"
result == Ok expected

# decode numbers
expect
phrase = "gvhgr mt123 gvhgr mt"
result = phrase |> decode
expected = "testing123testing"
result == Ok expected

# decode all the letters
expect
phrase = "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
result = phrase |> decode
expected = "thequickbrownfoxjumpsoverthelazydog"
result == Ok expected

# decode with too many spaces
expect
phrase = "vc vix r hn"
result = phrase |> decode
expected = "exercism"
result == Ok expected

# decode with no spaces
expect
phrase = "zmlyhgzxovrhlugvmzhgvkkrmthglmv"
result = phrase |> decode
expected = "anobstacleisoftenasteppingstone"
result == Ok expected

0 comments on commit 62abc6d

Please sign in to comment.