From 308147db14435f9fb362fb3b80771aa35f145d33 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Thu, 14 Aug 2025 10:08:07 +0000 Subject: [PATCH] [Sync Iteration] elixir/atbash-cipher/2 --- .../elixir/atbash-cipher/2/lib/atbash.ex | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 solutions/elixir/atbash-cipher/2/lib/atbash.ex diff --git a/solutions/elixir/atbash-cipher/2/lib/atbash.ex b/solutions/elixir/atbash-cipher/2/lib/atbash.ex new file mode 100644 index 0000000..462420f --- /dev/null +++ b/solutions/elixir/atbash-cipher/2/lib/atbash.ex @@ -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: <> + defp transpose(char) when char in ?a..?z, do: <> + defp transpose(_), do: "" +end