Avatar of heptalophos

heptalophos's solution

to Atbash Cipher in the Elm Track

Published at Jun 28 2019 · 0 comments
Test suite

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

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

$ elm-test

Automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the test suite, be sure to remove the skip <| calls from each test until you get them all passing!


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

Submitting Incomplete Solutions

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


module Tests exposing (tests)

import AtbashCipher exposing (decode, encode)
import Expect
import Test exposing (..)

tests : Test
tests =
    describe "AtbashCipher"
        [ test "encode no" <|
            \() -> Expect.equal "ml" (encode "no")
        , skip <|
            test "encode yes" <|
                \() -> Expect.equal "bvh" (encode "yes")
        , skip <|
            test "encode OMG" <|
                \() -> Expect.equal "lnt" (encode "OMG")
        , skip <|
            test "encode O M G" <|
                \() -> Expect.equal "lnt" (encode "O M G")
        , skip <|
            test "encode long word" <|
                \() -> Expect.equal "nrmwy oldrm tob" (encode "mindblowingly")
        , skip <|
            test "encode numbers" <|
                \() -> Expect.equal "gvhgr mt123 gvhgr mt" (encode "Testing, 1 2 3, testing.")
        , skip <|
            test "encode sentence" <|
                \() -> Expect.equal "gifgs rhurx grlm" (encode "Truth is fiction.")
        , skip <|
            test "encode all things" <|
                \() ->
                    Expect.equal "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
                        (encode "The quick brown fox jumps over the lazy dog.")
        , skip <|
            test "decode word" <|
                \() -> Expect.equal "exercism" (decode "vcvix rhn")
        , skip <|
            test "decode sentence" <|
                \() ->
                    Expect.equal "anobstacleisoftenasteppingstone"
                        (decode "zmlyh gzxov rhlug vmzhg vkkrm thglm v")
module AtbashCipher exposing (decode, encode)

chunkify : Int -> String -> String
chunkify n text =
    case text of 
        "" -> text
        _  -> (String.left n text) ++ " " ++ (chunkify n (String.dropLeft n text))

atbash : Char -> Char 
atbash ch =
    if Char.isAlpha ch then
        Char.fromCode((Char.toCode 'a') + (Char.toCode 'z') - Char.toCode ch)

encode : String -> String
encode plain = plain 
                |> String.toLower
                |> String.filter (\c -> Char.isLower c || Char.isDigit c)
                |> String.map atbash
                |> chunkify 5
                |> String.trim

decode : String -> String
decode cipher = cipher
                    |> String.filter (\c -> Char.isLower c || Char.isDigit c)
                    |> String.map atbash

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?