Skip to content

Add atbash-cipher exercise #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "atbash-cipher",
"name": "Atbash Cipher",
"uuid": "3da8ba25-b6a2-431f-a4a7-e7fc33318fdb",
"practices": [],
"prerequisites": [],
"difficulty": 2
}
]
},
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": [
"eNascimento178"
],
"files": {
"solution": [
"atbash-cipher.ijs"
],
"test": [
"test.ijs"
],
"example": [
".meta/example.ijs"
]
},
"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"
}
7 changes: 7 additions & 0 deletions exercises/practice/atbash-cipher/.meta/example.ijs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'strings'

numbers=: a. {~ 48 + i. 10
alphabet=: numbers ,~ |. a. {~ 97 + i. 26

encode=: (' ' joinstring _5 <\ alphabet {~ [: (75&+^:(0&>)@(_97 + a.&i.))"0 ('[^0-9a-z]';'')&rxrplc@tolower)
decode=: (a. {~ [: (_75&+^:(122&<)@(97 + alphabet&i.))"0 -.&' ')
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"
4 changes: 4 additions & 0 deletions exercises/practice/atbash-cipher/atbash-cipher.ijs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'general/unittest'

encode=: 'You need to implement this verb.'13!:8 (55)
decode=: 'You need to implement this verb.'13!:8 (55)
149 changes: 149 additions & 0 deletions exercises/practice/atbash-cipher/test.ijs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
load 'atbash-cipher.ijs'


before_all=: monad define
(]Description =: (3 : 'descriptions=: i.0')`(3 : 'descriptions=: descriptions , < y'))@.0 ''
(]Order =: (3 : 'order=: i.0')`(3 : 'order=: order , < y'))@.0 ''
(]Task =: (3 : 'tasks=: i.0')`(3 : 'tasks=: tasks , < y'))@.0 ''
)


atbash_cipher_test_01_ignore=: 0
test_atbash_cipher_test_01 =: monad define
Description@.1 ('encode yes')
Order@.1 (1)

NB. phrase=. 'yes'
NB. expected=. 'bvh'
assert 'bvh' -: encode 'yes'
)

atbash_cipher_test_02_ignore=: 1 NB. Change this value to 0 to run this test
test_atbash_cipher_test_02 =: monad define
Description@.1 ('encode no')
Order@.1 (2)

NB. phrase=. 'no'
NB. expected=. 'ml'
assert 'ml' -: encode 'no'
)

atbash_cipher_test_03_ignore=: 1 NB. Change this value to 0 to run this test
test_atbash_cipher_test_03 =: monad define
Description@.1 ('encode OMG')
Order@.1 (3)

NB. phrase=. 'OMG'
NB. expected=. 'lnt'
assert 'lnt' -: encode 'OMG'
)

atbash_cipher_test_04_ignore=: 1 NB. Change this value to 0 to run this test
test_atbash_cipher_test_04 =: monad define
Description@.1 ('encode spaces')
Order@.1 (4)

NB. phrase=. 'O M G'
NB. expected=. 'lnt'
assert 'lnt' -: encode 'O M G'
)

atbash_cipher_test_05_ignore=: 1 NB. Change this value to 0 to run this test
test_atbash_cipher_test_05 =: monad define
Description@.1 ('encode mindblowingly')
Order@.1 (5)

NB. phrase=. 'mindblowingly'
NB. expected=. 'nrmwy oldrm tob'
assert 'nrmwy oldrm tob' -: encode 'mindblowingly'
)

atbash_cipher_test_06_ignore=: 1 NB. Change this value to 0 to run this test
test_atbash_cipher_test_06 =: monad define
Description@.1 ('encode numbers')
Order@.1 (6)

NB. phrase=. 'Testing,1 2 3, testing.'
NB. expected=. 'gvhgr mt123 gvhgr mt'
assert 'gvhgr mt123 gvhgr mt' -: encode 'Testing,1 2 3, testing.'
)

atbash_cipher_test_07_ignore=: 1 NB. Change this value to 0 to run this test
test_atbash_cipher_test_07 =: monad define
Description@.1 ('encode deep thought')
Order@.1 (7)

NB. phrase=. 'Truth is fiction.'
NB. expected=. 'gifgs rhurx grlm'
assert 'gifgs rhurx grlm' -: encode 'Truth is fiction.'
)

atbash_cipher_test_08_ignore=: 1 NB. Change this value to 0 to run this test
test_atbash_cipher_test_08 =: monad define
Description@.1 ('encode all the letters')
Order@.1 (8)

NB. phrase=. 'The quick brown fox jumps over the lazy dog.'
NB. expected=. 'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt'
assert 'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt' -: encode 'The quick brown fox jumps over the lazy dog.'
)

atbash_cipher_test_09_ignore=: 1 NB. Change this value to 0 to run this test
test_atbash_cipher_test_09 =: monad define
Description@.1 ('decode exercism')
Order@.1 (9)

NB. phrase=. 'vcvix rhn'
NB. expected=. 'exercism'
assert 'exercism' -: decode 'vcvix rhn'
)

atbash_cipher_test_10_ignore=: 1 NB. Change this value to 0 to run this test
test_atbash_cipher_test_10 =: monad define
Description@.1 ('decode a sentence')
Order@.1 (10)

NB. phrase=. 'zmlyh gzxov rhlug vmzhg vkkrm thglm v'
NB. expected=. 'anobstacleisoftenasteppingstone'
assert 'anobstacleisoftenasteppingstone' -: decode 'zmlyh gzxov rhlug vmzhg vkkrm thglm v'
)

atbash_cipher_test_11_ignore=: 1 NB. Change this value to 0 to run this test
test_atbash_cipher_test_11 =: monad define
Description@.1 ('decode numbers')
Order@.1 (11)

NB. phrase=. 'gvhgr mt123 gvhgr mt'
NB. expected=. 'testing123testing'
assert 'testing123testing' -: decode 'gvhgr mt123 gvhgr mt'
)

atbash_cipher_test_12_ignore=: 1 NB. Change this value to 0 to run this test
test_atbash_cipher_test_12 =: monad define
Description@.1 ('decode all the letters')
Order@.1 (12)

NB. phrase=. 'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt'
NB. expected=. 'thequickbrownfoxjumpsoverthelazydog'
assert 'thequickbrownfoxjumpsoverthelazydog' -: decode 'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt'
)

atbash_cipher_test_13_ignore=: 1 NB. Change this value to 0 to run this test
test_atbash_cipher_test_13 =: monad define
Description@.1 ('decode with too many spaces')
Order@.1 (13)

NB. phrase=. 'vc vix r hn'
NB. expected=. 'exercism'
assert 'exercism' -: decode 'vc vix r hn'
)

atbash_cipher_test_14_ignore=: 1 NB. Change this value to 0 to run this test
test_atbash_cipher_test_14 =: monad define
Description@.1 ('decode with no spaces')
Order@.1 (14)

NB. phrase=. 'zmlyhgzxovrhlugvmzhgvkkrmthglmv'
NB. expected=. 'anobstacleisoftenasteppingstone'
assert 'anobstacleisoftenasteppingstone' -: decode 'zmlyhgzxovrhlugvmzhgvkkrmthglmv'
)