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

feat: solve with bin pattern matching and manual reduce #6

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
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ the letters A through Z (either lower or upper case) and whitespace. This way
data to be encoded will never contain any numbers and numbers inside data to
be decoded always represent the count for the following character.

## Debugging

- Put `require Logger` at the top of the `RunLengthEncoding` module.
- Log values to the terminal using `Logger.debug inspect <variable>`.
- Use the interactive shell to try out things by running `iex -S mix`.
- In `iex` you can reload your module using `r RunLengthEncoding`


## Running tests

Execute the tests with:
Expand All @@ -31,13 +39,6 @@ Execute the tests with:
$ mix test
```

## Debugging

- Put `require Logger` at the top of the `RunLengthEncoding` module.
- Log values to the terminal using `Logger.debug inspect <variable>`.
- Use the interactive shell to try out things by running `iex -S mix`.


### Pending tests

In the test suites, all but the first test have been skipped.
Expand Down
34 changes: 34 additions & 0 deletions lib/run_length_encoding.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,42 @@ defmodule RunLengthEncoding do
require Logger

def encode(string) do
string
|> encode_rec(0)
end

defp encode_rec(<<a::utf8, b::utf8>> <> rest, count) when a == b do
encode_rec(<<b::utf8>> <> rest, count + 1)
end

defp encode_rec(<<a::utf8>> <> rest, count) when count == 0 do
<<a::utf8>> <> encode_rec(rest, 0)
end

defp encode_rec(<<a::utf8>> <> rest, count) do
to_string(count + 1) <> <<a::utf8>> <> encode_rec(rest, 0)
end

defp encode_rec(rest, _), do: rest

def decode(string) do
string
|> decode_rec(0)
end

defguard is_digit(char) when ?0 <= char and char <= ?9

defp decode_rec(<<a::utf8>> <> rest, times) when is_digit(a) do
decode_rec(rest, times * 10 + String.to_integer(<<a>>))
end

defp decode_rec(<<a::utf8>> <> rest, 0) do
<<a>> <> decode_rec(rest, 0)
end

defp decode_rec(<<a::utf8>> <> rest, times) do
String.duplicate(<<a>>, times) <> decode_rec(rest, 0)
end

defp decode_rec(rest, _), do: rest
end
12 changes: 0 additions & 12 deletions test/run_length_encoding_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,52 @@ 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

@tag :pending
test "decode empty string" do
assert RunLengthEncoding.decode("") === ""
end

@tag :pending
test "decode single characters only" do
assert RunLengthEncoding.decode("XYZ") === "XYZ"
end

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

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

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

@tag :pending
test "decode lower case string" do
assert RunLengthEncoding.decode("2a3b4c") === "aabbbcccc"
end

@tag :pending
test "encode followed by decode gives original string" do
original = "zzz ZZ zZ"
encoded = RunLengthEncoding.encode(original)
Expand Down