Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rot 13 added #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
36 changes: 36 additions & 0 deletions lib/ciphers/rot13.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Algorithms.Ciphers.Rot13 do
@moduledoc """
subtition cipher for alphabets by rotating about the alphabet list 13 places (https://en.wikipedia.org/wiki/ROT13)

## Parameters

- text: Input number to insert the a new bit.

## Examples

iex> Rot13.encode("hello")
14
"""

def encode(string)

def encode(<<char>> <> tail) do
<<rotate(char)>> <> encode(tail)
end

def encode(_), do: ""

def rotate(char) when char >= ?a and char <= ?z do
?a + rem(char - ?a + 13, 26)
end

def rotate(char) when char >= ?A and char <= ?Z do
?A + rem(char - ?A + 13, 26)
end

def rotate(char), do: char

def decode(string) do
encode(string)
end
end
34 changes: 34 additions & 0 deletions test/ciphers/rot13.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule Algorithms.Ciphers.Rot13Test do
alias Algorithms.Ciphers.Rot13

use ExUnit.Case

test "encode and decode" do
assert Rot13.encode("Hello, World!") == "Uryyb, Jbeyq!"
assert Rot13.decode("Uryyb, Jbeyq!") == "Hello, World!"
end

test "encode and decode with uppercase letters" do
assert Rot13.encode("HELLO, WORLD!") == "URYYB, JBEYQ!"
assert Rot13.decode("URYYB, JBEYQ!") == "HELLO, WORLD!"
end

test "encode and decode with special characters" do
assert Rot13.encode("Hello, World! @#$%^&*()") == "Uryyb, Jbeyq! @#$%^&*()"
assert Rot13.decode("Uryyb, Jbeyq! @#$%^&*()") == "Hello, World! @#$%^&*()"
end

describe "Rot13.encode/1" do
test "encodes a string" do
assert Rot13.encode("Hello, World!") == "Uryyb, Jbeyq!"
end

test "encodes a string with uppercase letters" do
assert Rot13.encode("HELLO, WORLD!") == "URYYB, JBEYQ!"
end

test "encodes a string with special characters" do
assert Rot13.encode("Hello, World! @#$%^&*()") == "Uryyb, Jbeyq! @#$%^&*()"
end
end
end