ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# thekeele's solution

## to Atbash Cipher in the Elixir Track

Published at Dec 01 2019 · 0 comments
Instructions
Test suite
Solution

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:

``````Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: zyxwvutsrqponmlkjihgfedcba
``````

It is a very weak cipher because it only has one possible key, and it is a simple monoalphabetic 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, and punctuation is excluded. This is to make it harder to guess things based on word boundaries.

## Examples

• Encoding `test` gives `gvhg`
• Decoding `gvhg` gives `test`
• Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `thequickbrownfoxjumpsoverthelazydog`

## Running tests

Execute the tests with:

``````\$ mix test
``````

### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.

For example:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

## Source

Wikipedia http://en.wikipedia.org/wiki/Atbash

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### atbash_test.exs

``````defmodule AtbashTest do
use ExUnit.Case

describe "encode" do
# @tag :pending
test "yes" do
assert Atbash.encode("yes") == "bvh"
end

@tag :pending
test "no" do
assert Atbash.encode("no") == "ml"
end

@tag :pending
test "OMG" do
assert Atbash.encode("OMG") == "lnt"
end

@tag :pending
test "O M G" do
assert Atbash.encode("O M G") == "lnt"
end

@tag :pending
test "mindblowingly" do
assert Atbash.encode("mindblowingly") == "nrmwy oldrm tob"
end

@tag :pending
test "numbers" do
assert Atbash.encode("Testing, 1 2 3, testing.") == "gvhgr mt123 gvhgr mt"
end

@tag :pending
test "deep thought" do
assert Atbash.encode("Truth is fiction.") == "gifgs rhurx grlm"
end

@tag :pending
test "all the letters" do
plaintext = "The quick brown fox jumps over the lazy dog."
cipher = "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
assert Atbash.encode(plaintext) == cipher
end
end

describe "decode" do
@tag :pending
test "exercism" do
cipher = "vcvix rhn"
plaintext = "exercism"
assert Atbash.decode(cipher) == plaintext
end

@tag :pending
test "a sentence" do
cipher = "zmlyh gzxov rhlug vmzhg vkkrm thglm v"
plaintext = "anobstacleisoftenasteppingstone"
assert Atbash.decode(cipher) == plaintext
end

@tag :pending
test "numbers" do
cipher = "gvhgr mt123 gvhgr mt"
plaintext = "testing123testing"
assert Atbash.decode(cipher) == plaintext
end

@tag :pending
test "all the letters" do
cipher = "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
plaintext = "thequickbrownfoxjumpsoverthelazydog"
assert Atbash.decode(cipher) == plaintext
end

@tag :pending
test "with too many spaces" do
cipher = "vc vix    r hn"
plaintext = "exercism"
assert Atbash.decode(cipher) == plaintext
end

@tag :pending
test "with no spaces" do
cipher = "zmlyhgzxovrhlugvmzhgvkkrmthglmv"
plaintext = "anobstacleisoftenasteppingstone"
assert Atbash.decode(cipher) == plaintext
end
end
end``````

### test_helper.exs

``````ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)``````
``````defmodule Atbash do
@spec encode(String.t()) :: String.t()
def encode(plaintext) do
plaintext |> substitute() |> Enum.chunk_every(5) |> Enum.join(" ")
end

@spec decode(String.t()) :: String.t()
def decode(cipher) do
cipher |> substitute() |> to_string()
end

defp substitute(text) when is_binary(text),
do: text |> String.downcase() |> to_charlist |> Enum.reduce([], &substitute/2)
defp substitute(letter, cipher) when letter in ?a..?z,
do: cipher ++ [(?a + ?z) - letter]
defp substitute(number, cipher) when number in ?0..?9,
do: cipher ++ [number]
defp substitute(_, cipher),
do: cipher
end``````