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
# TODO: Replace examples and use TDD by writing your own tests
defmodule TestSolution do
use ExUnit.Case
import Rot13, only: [encode: 1]
test "some test description" do
assert encode("NOWHERE abjurer") == "ABJURER nowhere"
assert encode(" stuff ") == " fghss "
assert encode("123 mirë u pafshim абв") == "123 zveë h cnsfuvz абв"
end
end