Avatar of davearonson

davearonson's solution

to Atbash Cipher in the Elixir Track

Published at Jul 13 2018 · 0 comments
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.


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

Running tests

Execute the tests with:

$ elixir atbash_cipher_test.exs

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!"

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

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

For more detailed information about the Elixir track, please see the help page.


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.


if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("atbash_cipher.exs", __DIR__)

ExUnit.configure(exclude: :pending, trace: true)

defmodule AtbashTest do
  use ExUnit.Case

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

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

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

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

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

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

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

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

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

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

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

  @tag :pending
  test "decode all the letters" do
    cipher = "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
    plaintext = "thequickbrownfoxjumpsoverthelazydog"
    assert Atbash.decode(cipher) == plaintext
defmodule Atbash do
  @doc """
  Encode a given plaintext to the corresponding ciphertext

  ## Examples

  iex> Atbash.encode("completely insecure")
  "xlnko vgvob rmhvx fiv"
  @spec encode(String.t) :: String.t
  def encode(plaintext) do
    Regex.scan(~r/[a-zA-Z0-9]/, String.downcase(plaintext))
    |> Enum.map(&List.first/1)
    |> Enum.map(&(transform(&1)))
    |> Enum.chunk(5, 5, [])
    |> Enum.map(&Enum.join/1)
    |> Enum.join(" ")

  defp transform(char) do
    cond do
      Regex.match?(~r/[a-z]/, char) ->
        [?z + ?a - (char |> to_charlist |> List.first)]
      Regex.match?(~r/[0-9]/, char) ->
      true ->
        IO.puts "Transforming #{inspect char}"
        raise ArgumentError


Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?