-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement run-length-encoding in Elixir
- Loading branch information
1 parent
6c4524e
commit 905197c
Showing
8 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# The directory Mix will write compiled artifacts to. | ||
/_build/ | ||
|
||
# If you run "mix test --cover", coverage assets end up here. | ||
/cover/ | ||
|
||
# The directory Mix downloads your dependencies sources to. | ||
/deps/ | ||
|
||
# Where third-party dependencies like ExDoc output generated docs. | ||
/doc/ | ||
|
||
# Ignore .fetch files in case you like to edit your project deps locally. | ||
/.fetch | ||
|
||
# If the VM crashes, it generates a dump, let's ignore it too. | ||
erl_crash.dump | ||
|
||
# Also ignore archive artifacts (built via "mix archive.build"). | ||
*.ez | ||
|
||
# Ignore package tarball (built via "mix hex.build"). | ||
run_length_encoding-*.tar | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Help | ||
|
||
## Running the tests | ||
|
||
From the terminal, change to the base directory of the exercise then execute the tests with: | ||
|
||
```bash | ||
$ mix test | ||
``` | ||
|
||
This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs` | ||
|
||
Documentation: | ||
|
||
* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html) | ||
* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html) | ||
|
||
## Pending tests | ||
|
||
In test suites of practice exercises, all but the first test have been tagged to be skipped. | ||
|
||
Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol. | ||
|
||
For example: | ||
|
||
```elixir | ||
# @tag :pending | ||
test "shouting" do | ||
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!" | ||
end | ||
``` | ||
|
||
If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command: | ||
|
||
```bash | ||
$ mix test --include pending | ||
``` | ||
|
||
Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`. | ||
|
||
```elixir | ||
# ExUnit.configure(exclude: :pending, trace: true) | ||
``` | ||
|
||
## Useful `mix test` options | ||
|
||
* `test/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.exs` whose definition is on line `LINENUM` | ||
* `--failed` - runs only tests that failed the last time they ran | ||
* `--max-failures` - the suite stops evaluating tests when this number of test failures | ||
is reached | ||
* `--seed 0` - disables randomization so the tests in a single file will always be ran | ||
in the same order they were defined in | ||
|
||
## Submitting your solution | ||
|
||
You can submit your solution using the `exercism submit lib/run_length_encoder.ex` command. | ||
This command will upload your solution to the Exercism website and print the solution page's URL. | ||
|
||
It's possible to submit an incomplete solution which allows you to: | ||
|
||
- See how others have completed the exercise | ||
- Request help from a mentor | ||
|
||
## Need to get help? | ||
|
||
If you'd like help solving the exercise, check the following pages: | ||
|
||
- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir) | ||
- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir) | ||
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5) | ||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) | ||
|
||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. | ||
|
||
If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Run-Length Encoding | ||
|
||
Welcome to Run-Length Encoding on Exercism's Elixir Track. | ||
If you need help running the tests or submitting your code, check out `HELP.md`. | ||
|
||
## Instructions | ||
|
||
Implement run-length encoding and decoding. | ||
|
||
Run-length encoding (RLE) is a simple form of data compression, where runs (consecutive data elements) are replaced by just one data value and count. | ||
|
||
For example we can represent the original 53 characters with only 13. | ||
|
||
```text | ||
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" -> "12WB12W3B24WB" | ||
``` | ||
|
||
RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression. | ||
|
||
```text | ||
"AABCCCDEEEE" -> "2AB3CD4E" -> "AABCCCDEEEE" | ||
``` | ||
|
||
For simplicity, you can assume that the unencoded string will only contain 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. | ||
|
||
## Source | ||
|
||
### Created by | ||
|
||
- @Teapane | ||
|
||
### Contributed to by | ||
|
||
- @angelikatyborska | ||
- @CoderDennis | ||
- @Cohen-Carlisle | ||
- @dalexj | ||
- @daveyarwood | ||
- @devonestes | ||
- @lex57ukr | ||
- @lpil | ||
- @neenjaw | ||
- @parkerl | ||
- @sotojuan | ||
- @waiting-for-dev | ||
|
||
### Based on | ||
|
||
Wikipedia - https://en.wikipedia.org/wiki/Run-length_encoding |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
defmodule RunLengthEncoder 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" | ||
""" | ||
@spec encode(String.t()) :: String.t() | ||
def encode(string) do | ||
string | ||
|> String.codepoints() | ||
|> Enum.reduce({"", 1, ""}, fn | ||
char, {current_elem, count, result} when current_elem == char -> | ||
{current_elem, count + 1, result} | ||
|
||
char, {current_elem, 1, result} -> | ||
{char, 1, result <> current_elem} | ||
|
||
char, {current_elem, count, result} -> | ||
{char, 1, "#{result}#{count}#{current_elem}"} | ||
end) | ||
|> then(fn | ||
{current_elem, 1, result} -> | ||
result <> current_elem | ||
|
||
{current_elem, count, result} -> | ||
"#{result}#{count}#{current_elem}" | ||
end) | ||
end | ||
|
||
@spec decode(String.t()) :: String.t() | ||
def decode(string) do | ||
string | ||
|> String.codepoints() | ||
|> Enum.reduce({"0", ""}, fn | ||
char, {current_num, result} -> | ||
case Integer.parse(char) do | ||
:error -> | ||
repeat = | ||
case Integer.parse(current_num) do | ||
{0, _} -> 1 | ||
{num, _} -> num | ||
end | ||
|
||
{"0", result <> String.duplicate(char, repeat)} | ||
|
||
_ -> | ||
{current_num <> char, result} | ||
end | ||
end) | ||
|> then(fn {_, result} -> result end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule RunLengthEncoder.MixProject do | ||
use Mix.Project | ||
|
||
def project do | ||
[ | ||
app: :run_length_encoder, | ||
version: "0.1.0", | ||
# elixir: "~> 1.8", | ||
start_permanent: Mix.env() == :prod, | ||
deps: deps() | ||
] | ||
end | ||
|
||
# Run "mix help compile.app" to learn about applications. | ||
def application do | ||
[ | ||
extra_applications: [:logger] | ||
] | ||
end | ||
|
||
# Run "mix help deps" to learn about dependencies. | ||
defp deps do | ||
[ | ||
# {:dep_from_hexpm, "~> 0.3.0"}, | ||
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} | ||
] | ||
end | ||
end |
59 changes: 59 additions & 0 deletions
59
elixir/run-length-encoding/test/run_length_encoder_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
defmodule RunLengthEncoderTest do | ||
use ExUnit.Case | ||
|
||
test "encode empty string" do | ||
assert RunLengthEncoder.encode("") === "" | ||
end | ||
|
||
test "encode single characters only are encoded without count" do | ||
assert RunLengthEncoder.encode("XYZ") === "XYZ" | ||
end | ||
|
||
test "encode string with no single characters" do | ||
assert RunLengthEncoder.encode("AABBBCCCC") == "2A3B4C" | ||
end | ||
|
||
test "encode single characters mixed with repeated characters" do | ||
assert RunLengthEncoder.encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB") === | ||
"12WB12W3B24WB" | ||
end | ||
|
||
test "encode multiple whitespace mixed in string" do | ||
assert RunLengthEncoder.encode(" hsqq qww ") === "2 hs2q q2w2 " | ||
end | ||
|
||
test "encode lowercase characters" do | ||
assert RunLengthEncoder.encode("aabbbcccc") === "2a3b4c" | ||
end | ||
|
||
test "decode empty string" do | ||
assert RunLengthEncoder.decode("") === "" | ||
end | ||
|
||
test "decode single characters only" do | ||
assert RunLengthEncoder.decode("XYZ") === "XYZ" | ||
end | ||
|
||
test "decode string with no single characters" do | ||
assert RunLengthEncoder.decode("2A3B4C") == "AABBBCCCC" | ||
end | ||
|
||
test "decode single characters with repeated characters" do | ||
assert RunLengthEncoder.decode("12WB12W3B24WB") === | ||
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" | ||
end | ||
|
||
test "decode multiple whitespace mixed in string" do | ||
assert RunLengthEncoder.decode("2 hs2q q2w2 ") === " hsqq qww " | ||
end | ||
|
||
test "decode lower case string" do | ||
assert RunLengthEncoder.decode("2a3b4c") === "aabbbcccc" | ||
end | ||
|
||
test "encode followed by decode gives original string" do | ||
original = "zzz ZZ zZ" | ||
encoded = RunLengthEncoder.encode(original) | ||
assert RunLengthEncoder.decode(encoded) === original | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ExUnit.start() | ||
ExUnit.configure(exclude: :pending, trace: true) |