From dad68cb89f7bb8d84feffa589b56ae033df50e89 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Sat, 29 Jun 2024 02:14:21 -0400 Subject: [PATCH] run-length-encoder --- .../run-length-encoding/.exercism/config.json | 33 ++++++++ .../.exercism/metadata.json | 1 + elixir/run-length-encoding/.formatter.exs | 4 + elixir/run-length-encoding/.gitignore | 24 ++++++ elixir/run-length-encoding/HELP.md | 75 +++++++++++++++++++ elixir/run-length-encoding/README.md | 50 +++++++++++++ .../lib/run_length_encoder.ex | 46 ++++++++++++ elixir/run-length-encoding/mix.exs | 28 +++++++ .../test/run_length_encoder_test.exs | 59 +++++++++++++++ .../run-length-encoding/test/test_helper.exs | 2 + 10 files changed, 322 insertions(+) create mode 100644 elixir/run-length-encoding/.exercism/config.json create mode 100644 elixir/run-length-encoding/.exercism/metadata.json create mode 100644 elixir/run-length-encoding/.formatter.exs create mode 100644 elixir/run-length-encoding/.gitignore create mode 100644 elixir/run-length-encoding/HELP.md create mode 100644 elixir/run-length-encoding/README.md create mode 100644 elixir/run-length-encoding/lib/run_length_encoder.ex create mode 100644 elixir/run-length-encoding/mix.exs create mode 100644 elixir/run-length-encoding/test/run_length_encoder_test.exs create mode 100644 elixir/run-length-encoding/test/test_helper.exs diff --git a/elixir/run-length-encoding/.exercism/config.json b/elixir/run-length-encoding/.exercism/config.json new file mode 100644 index 0000000..72e2d35 --- /dev/null +++ b/elixir/run-length-encoding/.exercism/config.json @@ -0,0 +1,33 @@ +{ + "authors": [ + "Teapane" + ], + "contributors": [ + "angelikatyborska", + "CoderDennis", + "Cohen-Carlisle", + "dalexj", + "daveyarwood", + "devonestes", + "lex57ukr", + "lpil", + "neenjaw", + "parkerl", + "sotojuan", + "waiting-for-dev" + ], + "files": { + "solution": [ + "lib/run_length_encoder.ex" + ], + "test": [ + "test/run_length_encoder_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Implement run-length encoding and decoding.", + "source": "Wikipedia", + "source_url": "https://en.wikipedia.org/wiki/Run-length_encoding" +} diff --git a/elixir/run-length-encoding/.exercism/metadata.json b/elixir/run-length-encoding/.exercism/metadata.json new file mode 100644 index 0000000..f651217 --- /dev/null +++ b/elixir/run-length-encoding/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"run-length-encoding","id":"df85d72f5fcc49efad7a6f2e236c7872","url":"https://exercism.org/tracks/elixir/exercises/run-length-encoding","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/run-length-encoding/.formatter.exs b/elixir/run-length-encoding/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/run-length-encoding/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/run-length-encoding/.gitignore b/elixir/run-length-encoding/.gitignore new file mode 100644 index 0000000..64654ae --- /dev/null +++ b/elixir/run-length-encoding/.gitignore @@ -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 + diff --git a/elixir/run-length-encoding/HELP.md b/elixir/run-length-encoding/HELP.md new file mode 100644 index 0000000..e943ba1 --- /dev/null +++ b/elixir/run-length-encoding/HELP.md @@ -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/.exs:LINENUM` - runs only a single test, the test from `.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. \ No newline at end of file diff --git a/elixir/run-length-encoding/README.md b/elixir/run-length-encoding/README.md new file mode 100644 index 0000000..d6d3031 --- /dev/null +++ b/elixir/run-length-encoding/README.md @@ -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 \ No newline at end of file diff --git a/elixir/run-length-encoding/lib/run_length_encoder.ex b/elixir/run-length-encoding/lib/run_length_encoder.ex new file mode 100644 index 0000000..2e302b5 --- /dev/null +++ b/elixir/run-length-encoding/lib/run_length_encoder.ex @@ -0,0 +1,46 @@ +defmodule RunLengthEncoder do + @digits ~w(0 1 2 3 4 5 6 7 8 9) + + defguardp is_digit(char) when char in @digits + + defp encode_reducer(char, []), do: [{char, 1}] + defp encode_reducer(char, [{char, count} | rest]), do: [{char, count + 1} | rest] + defp encode_reducer(char, rest), do: [{char, 1} | rest] + + defp encode_mapper({char, 1}), do: char + defp encode_mapper({char, count}), do: "#{count}#{char}" + + defp decode_reducer(digit, []) when is_digit(digit), do: [{nil, digit}] + defp decode_reducer(digit, [{nil, count} | rest]) when is_digit(digit), do: [{nil, count <> digit} | rest] + defp decode_reducer(char, [{nil, count} | rest]) when not is_digit(char), do: [{char, String.to_integer(count)} | rest] + defp decode_reducer(digit, rest) when is_digit(digit), do: [{nil, digit} | rest] + defp decode_reducer(char, rest), do: [{char, 1} | rest] + + defp decode_mapper({char, 1}), do: char + defp decode_mapper({char, count}), do: String.duplicate(char, count) + + @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.graphemes() + |> Enum.reduce([], &encode_reducer/2) + |> Enum.reverse() + |> Enum.map_join(&encode_mapper/1) + end + + @spec decode(String.t()) :: String.t() + def decode(string) do + string + |> String.graphemes() + |> Enum.reduce([], &decode_reducer/2) + |> Enum.reverse() + |> Enum.map_join(&decode_mapper/1) + end +end diff --git a/elixir/run-length-encoding/mix.exs b/elixir/run-length-encoding/mix.exs new file mode 100644 index 0000000..24d8832 --- /dev/null +++ b/elixir/run-length-encoding/mix.exs @@ -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 diff --git a/elixir/run-length-encoding/test/run_length_encoder_test.exs b/elixir/run-length-encoding/test/run_length_encoder_test.exs new file mode 100644 index 0000000..7fff75d --- /dev/null +++ b/elixir/run-length-encoding/test/run_length_encoder_test.exs @@ -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 diff --git a/elixir/run-length-encoding/test/test_helper.exs b/elixir/run-length-encoding/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/run-length-encoding/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)