Sha256: 8ba442cd70d23d5caafe1008ebfca5ad0850eebeaeca525c66b2363cf4f611d1

Contents?: true

Size: 1.29 KB

Versions: 66

Compression:

Stored size: 1.29 KB

Contents

module AtbashCipher exposing (decode, encode)

import Char
import Dict
import Regex exposing (HowMany(All), regex)
import String


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

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.179 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.178 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.177 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.176 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.175 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.174 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.173 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.172 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.171 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.170 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.169 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.167 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.166 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.165 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.164 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.163 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.162 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.161 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm
trackler-2.2.1.160 tracks/elm/exercises/atbash-cipher/AtbashCipher.example.elm