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

encode function #7

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
20 changes: 10 additions & 10 deletions lib/run_length_encoding.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
defmodule RunLengthEncoding do
@doc """
Generates a string where consecutive elements are represented as a data value and count.
"AABBBCCCC" => "2A3B4C"
For this example, assume all input are strings, that are all uppercase letters.
It should also be able to reconstruct the data into its original form.
"2A3B4C" => "AABBBCCCC"
"""
require Logger
require IEx

def encode(string) do
def encode(string) when string == "" do
""
end

def decode(string) do
def encode(string) do
%{"head" => head, "tail" => tail} = Regex.named_captures(~r/(?<head>#{String.at(string,0)}+)(?<tail>.*)/, string)
if String.length(head) == 1 do
"#{head}#{encode(tail)}"
else
"#{String.length(head)}#{String.at(head, 0)}#{encode(tail)}"
end
end
end
5 changes: 0 additions & 5 deletions test/run_length_encoding_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,23 @@ defmodule RunLengthEncodingTest do
assert RunLengthEncoding.encode("") === ""
end

@tag :pending
test "encode single characters only are encoded without count" do
assert RunLengthEncoding.encode("XYZ") === "XYZ"
end

@tag :pending
test "encode string with no single characters" do
assert RunLengthEncoding.encode("AABBBCCCC") == "2A3B4C"
end

@tag :pending
test "encode single characters mixed with repeated characters" do
assert RunLengthEncoding.encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB") ===
"12WB12W3B24WB"
end

@tag :pending
test "encode multiple whitespace mixed in string" do
assert RunLengthEncoding.encode(" hsqq qww ") === "2 hs2q q2w2 "
end

@tag :pending
test "encode lowercase characters" do
assert RunLengthEncoding.encode("aabbbcccc") === "2a3b4c"
end
Expand Down