Add atbash-cipher exercise

This commit is contained in:
Erik Simmler 2016-04-28 21:03:30 -04:00
parent 889e06279c
commit 72a56f6e12
8 changed files with 128 additions and 2 deletions

View file

@ -27,7 +27,8 @@
"grade-school", "grade-school",
"allergies", "allergies",
"robot-simulator", "robot-simulator",
"list-ops" "list-ops",
"atbash-cipher"
], ],
"deprecated": [ "deprecated": [

View file

@ -28,7 +28,8 @@
"./exercises/grade-school", "./exercises/grade-school",
"./exercises/allergies", "./exercises/allergies",
"./exercises/robot-simulator", "./exercises/robot-simulator",
"./exercises/list-ops" "./exercises/list-ops",
"./exercises/atbash-cipher"
], ],
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {

View file

@ -0,0 +1 @@
module AtbashCipher exposing (..)

View file

@ -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

View file

@ -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

View file

@ -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"
}

View file

@ -0,0 +1,4 @@
@echo off
for %%f in (*Tests.elm) do (
elm-make %%f --yes --output build.js && node build.js
)

View file

@ -0,0 +1,2 @@
#!/usr/bin/env bash
elm-make *Tests.elm --yes --output build.js && node build.js