From 72a56f6e12637b4672760798e77a38e9a4233046 Mon Sep 17 00:00:00 2001 From: Erik Simmler Date: Thu, 28 Apr 2016 21:03:30 -0400 Subject: [PATCH] Add atbash-cipher exercise --- config.json | 3 +- elm-package.json | 3 +- exercises/atbash-cipher/AtbashCipher.elm | 1 + exercises/atbash-cipher/AtbashCipher.example | 63 +++++++++++++++++++ exercises/atbash-cipher/AtbashCipherTests.elm | 39 ++++++++++++ exercises/atbash-cipher/elm-package.json | 15 +++++ exercises/atbash-cipher/runtests.bat | 4 ++ exercises/atbash-cipher/runtests.sh | 2 + 8 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 exercises/atbash-cipher/AtbashCipher.elm create mode 100644 exercises/atbash-cipher/AtbashCipher.example create mode 100644 exercises/atbash-cipher/AtbashCipherTests.elm create mode 100644 exercises/atbash-cipher/elm-package.json create mode 100755 exercises/atbash-cipher/runtests.bat create mode 100755 exercises/atbash-cipher/runtests.sh diff --git a/config.json b/config.json index ff99925..ff9b2d3 100644 --- a/config.json +++ b/config.json @@ -27,7 +27,8 @@ "grade-school", "allergies", "robot-simulator", - "list-ops" + "list-ops", + "atbash-cipher" ], "deprecated": [ diff --git a/elm-package.json b/elm-package.json index ed0fb07..e7fc017 100644 --- a/elm-package.json +++ b/elm-package.json @@ -28,7 +28,8 @@ "./exercises/grade-school", "./exercises/allergies", "./exercises/robot-simulator", - "./exercises/list-ops" + "./exercises/list-ops", + "./exercises/atbash-cipher" ], "exposed-modules": [], "dependencies": { diff --git a/exercises/atbash-cipher/AtbashCipher.elm b/exercises/atbash-cipher/AtbashCipher.elm new file mode 100644 index 0000000..3d5c6d2 --- /dev/null +++ b/exercises/atbash-cipher/AtbashCipher.elm @@ -0,0 +1 @@ +module AtbashCipher exposing (..) diff --git a/exercises/atbash-cipher/AtbashCipher.example b/exercises/atbash-cipher/AtbashCipher.example new file mode 100644 index 0000000..dab565c --- /dev/null +++ b/exercises/atbash-cipher/AtbashCipher.example @@ -0,0 +1,63 @@ +module AtbashCipher exposing (..) + +import String +import Dict +import Char +import Regex exposing (HowMany(All), regex) + + +encode : String -> String +encode plain = + let + translate = + toTranslator alphabet reversedAlphabet + in + plain + |> String.toLower + |> String.filter (\c -> Char.isLower c || Char.isDigit c) + |> String.map translate + |> insertEvery 5 " " + + +decode : String -> String +decode cipher = + let + translate = + toTranslator reversedAlphabet alphabet + in + cipher + |> String.filter ((/=) ' ') + |> String.map translate + + +alphabet : String +alphabet = + "abcdefghijklmnopqrstuvwxyz" + + +reversedAlphabet : String +reversedAlphabet = + -- AKA tebahpla + String.reverse alphabet + + +toTranslator : String -> String -> Char -> Char +toTranslator from to = + let + table = + List.map2 (,) (String.toList from) (String.toList to) + |> Dict.fromList + + translate key = + Dict.get key table + |> Maybe.withDefault key + in + translate + + +insertEvery : Int -> String -> String -> String +insertEvery size insertion string = + Regex.replace All + (regex (".{" ++ toString size ++ "}(?!$)")) + (\{ match } -> match ++ insertion) + string diff --git a/exercises/atbash-cipher/AtbashCipherTests.elm b/exercises/atbash-cipher/AtbashCipherTests.elm new file mode 100644 index 0000000..5ef2ffb --- /dev/null +++ b/exercises/atbash-cipher/AtbashCipherTests.elm @@ -0,0 +1,39 @@ +module Main exposing (..) + +import ElmTest exposing (..) +import AtbashCipher exposing (encode, decode) + + +tests : Test +tests = + suite "AtbashCipher" + [ test "encode no" + (assertEqual "ml" (encode "no")) + , test "encode yes" + (assertEqual "bvh" (encode "yes")) + , test "encode OMG" + (assertEqual "lnt" (encode "OMG")) + , test "encode O M G" + (assertEqual "lnt" (encode "O M G")) + , test "encode long word" + (assertEqual "nrmwy oldrm tob" (encode "mindblowingly")) + , test "encode numbers" + (assertEqual "gvhgr mt123 gvhgr mt" (encode "Testing, 1 2 3, testing.")) + , test "encode sentence" + (assertEqual "gifgs rhurx grlm" (encode "Truth is fiction.")) + , test "encode all things" + (assertEqual "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt" + (encode "The quick brown fox jumps over the lazy dog.") + ) + , test "decode word" + (assertEqual "exercism" (decode "vcvix rhn")) + , test "decode sentence" + (assertEqual "anobstacleisoftenasteppingstone" + (decode "zmlyh gzxov rhlug vmzhg vkkrm thglm v") + ) + ] + + +main : Program Never +main = + runSuite tests diff --git a/exercises/atbash-cipher/elm-package.json b/exercises/atbash-cipher/elm-package.json new file mode 100644 index 0000000..1ab2bdf --- /dev/null +++ b/exercises/atbash-cipher/elm-package.json @@ -0,0 +1,15 @@ +{ + "version": "2.0.0", + "summary": "Exercism problems in Elm.", + "repository": "https://github.com/exercism/xelm.git", + "license": "BSD3", + "source-directories": [ + "." + ], + "exposed-modules": [], + "dependencies": { + "elm-community/elm-test": "1.0.0 <= v < 2.0.0", + "elm-lang/core": "4.0.0 <= v < 5.0.0" + }, + "elm-version": "0.17.0 <= v < 0.18.0" +} diff --git a/exercises/atbash-cipher/runtests.bat b/exercises/atbash-cipher/runtests.bat new file mode 100755 index 0000000..2a38cd8 --- /dev/null +++ b/exercises/atbash-cipher/runtests.bat @@ -0,0 +1,4 @@ +@echo off +for %%f in (*Tests.elm) do ( + elm-make %%f --yes --output build.js && node build.js +) diff --git a/exercises/atbash-cipher/runtests.sh b/exercises/atbash-cipher/runtests.sh new file mode 100755 index 0000000..2bed7b2 --- /dev/null +++ b/exercises/atbash-cipher/runtests.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +elm-make *Tests.elm --yes --output build.js && node build.js