mirror of
https://github.com/correl/elm.git
synced 2024-11-15 19:19:31 +00:00
Add atbash-cipher exercise
This commit is contained in:
parent
889e06279c
commit
72a56f6e12
8 changed files with 128 additions and 2 deletions
|
@ -27,7 +27,8 @@
|
|||
"grade-school",
|
||||
"allergies",
|
||||
"robot-simulator",
|
||||
"list-ops"
|
||||
"list-ops",
|
||||
"atbash-cipher"
|
||||
],
|
||||
"deprecated": [
|
||||
|
||||
|
|
|
@ -28,7 +28,8 @@
|
|||
"./exercises/grade-school",
|
||||
"./exercises/allergies",
|
||||
"./exercises/robot-simulator",
|
||||
"./exercises/list-ops"
|
||||
"./exercises/list-ops",
|
||||
"./exercises/atbash-cipher"
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
|
|
1
exercises/atbash-cipher/AtbashCipher.elm
Normal file
1
exercises/atbash-cipher/AtbashCipher.elm
Normal file
|
@ -0,0 +1 @@
|
|||
module AtbashCipher exposing (..)
|
63
exercises/atbash-cipher/AtbashCipher.example
Normal file
63
exercises/atbash-cipher/AtbashCipher.example
Normal 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
|
39
exercises/atbash-cipher/AtbashCipherTests.elm
Normal file
39
exercises/atbash-cipher/AtbashCipherTests.elm
Normal 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
|
15
exercises/atbash-cipher/elm-package.json
Normal file
15
exercises/atbash-cipher/elm-package.json
Normal 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"
|
||||
}
|
4
exercises/atbash-cipher/runtests.bat
Executable file
4
exercises/atbash-cipher/runtests.bat
Executable file
|
@ -0,0 +1,4 @@
|
|||
@echo off
|
||||
for %%f in (*Tests.elm) do (
|
||||
elm-make %%f --yes --output build.js && node build.js
|
||||
)
|
2
exercises/atbash-cipher/runtests.sh
Executable file
2
exercises/atbash-cipher/runtests.sh
Executable file
|
@ -0,0 +1,2 @@
|
|||
#!/usr/bin/env bash
|
||||
elm-make *Tests.elm --yes --output build.js && node build.js
|
Loading…
Reference in a new issue