diff --git a/solutions/elixir/rotational-cipher/2/lib/rotational_cipher.ex b/solutions/elixir/rotational-cipher/2/lib/rotational_cipher.ex new file mode 100644 index 0000000..322dcd9 --- /dev/null +++ b/solutions/elixir/rotational-cipher/2/lib/rotational_cipher.ex @@ -0,0 +1,18 @@ +defmodule RotationalCipher do + @doc """ + Given a plaintext and amount to shift by, return a rotated string. + + Example: + iex> RotationalCipher.rotate("Attack at dawn", 13) + "Nggnpx ng qnja" + """ + @spec rotate(text :: String.t(), shift :: integer) :: String.t() + def rotate(text, shift) do + shift_char = fn + char when char in ?A..?Z -> <> + char when char in ?a..?z -> <> + char -> <> + end + text |> String.to_charlist() |> Enum.map_join(shift_char) + end +end