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.
gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwltgives
Refer to the Installing Elm page for information about installing elm.
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:
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
calls from each test until you get them all passing!
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) encode : String -> String encode = String.toLower >> String.filter Char.isAlphaNum >> String.map crypt >> group decode : String -> String decode = String.filter Char.isAlphaNum >> String.map crypt crypt : Char -> Char crypt char = if Char.isDigit char then char else char |> Char.toCode |> (-) (Char.toCode 'a') |> (+) (Char.toCode 'z') |> Char.fromCode group : String -> String group string = if String.length string > 5 then String.concat [ String.left 5 string , " " , group (String.dropLeft 5 string) ] else string
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.