Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions solutions/elixir/atbash-cipher/2/lib/atbash.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Atbash do
@doc """
Encode a given plaintext to the corresponding ciphertext

## Examples

iex> Atbash.encode("completely insecure")
"xlnko vgvob rmhvx fiv"
"""
@spec encode(String.t()) :: String.t()
def encode(plaintext) do
plaintext
|> String.downcase()
|> String.to_charlist()
|> Enum.map_join(&transpose/1)
|> String.replace(~r/(.{5})\B/, "\\1 ")
end

@spec decode(String.t()) :: String.t()
def decode(cipher) do
cipher
|> String.to_charlist()
|> Enum.map_join(&transpose/1)
end

defp transpose(char) when char in ?0..?9, do: <<char>>
defp transpose(char) when char in ?a..?z, do: <<?a + ?z - char>>
defp transpose(_), do: ""
end