Ad
Encoding
Algorithms
Logic

Create a function, that takes string as its only argument and yields rot13-encoded string. Only standard latin letters must be affected, avoid touching other characters and whitespaces.

defmodule Rot13 do
  def rotate({k, v}) do
    cond do
      v in ?A..?M ->
        k + 13

      v in ?N..?Z ->
        k - 13

      true ->
        k
    end
  end
  
  def encode(str) do
    List.zip([
      str
      |> String.to_charlist(),
      str
      |> String.upcase()
      |> String.to_charlist()
    ])
    |> Enum.map(&rotate/1)
    |> List.to_string()
  end
end