diff --git a/lib/run_length_encoding.ex b/lib/run_length_encoding.ex
index d32c5a3..02a7a6c 100644
--- a/lib/run_length_encoding.ex
+++ b/lib/run_length_encoding.ex
@@ -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/(?
#{String.at(string,0)}+)(?.*)/, string)
+ if String.length(head) == 1 do
+ "#{head}#{encode(tail)}"
+ else
+ "#{String.length(head)}#{String.at(head, 0)}#{encode(tail)}"
+ end
end
end
diff --git a/test/run_length_encoding_test.exs b/test/run_length_encoding_test.exs
index 68ca48c..4301805 100644
--- a/test/run_length_encoding_test.exs
+++ b/test/run_length_encoding_test.exs
@@ -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