From 81b389528e0579bf6bd82dee005e3ac5e9d11917 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Tue, 2 Jul 2024 12:47:44 -0400 Subject: [PATCH] flatten_array --- elixir/flatten-array/.exercism/config.json | 29 +++++++ elixir/flatten-array/.exercism/metadata.json | 1 + elixir/flatten-array/.formatter.exs | 4 + elixir/flatten-array/.gitignore | 24 ++++++ elixir/flatten-array/HELP.md | 75 +++++++++++++++++++ elixir/flatten-array/README.md | 37 +++++++++ elixir/flatten-array/lib/flatten_array.ex | 21 ++++++ elixir/flatten-array/mix.exs | 28 +++++++ .../flatten-array/test/flatten_array_test.exs | 58 ++++++++++++++ elixir/flatten-array/test/test_helper.exs | 2 + 10 files changed, 279 insertions(+) create mode 100644 elixir/flatten-array/.exercism/config.json create mode 100644 elixir/flatten-array/.exercism/metadata.json create mode 100644 elixir/flatten-array/.formatter.exs create mode 100644 elixir/flatten-array/.gitignore create mode 100644 elixir/flatten-array/HELP.md create mode 100644 elixir/flatten-array/README.md create mode 100644 elixir/flatten-array/lib/flatten_array.ex create mode 100644 elixir/flatten-array/mix.exs create mode 100644 elixir/flatten-array/test/flatten_array_test.exs create mode 100644 elixir/flatten-array/test/test_helper.exs diff --git a/elixir/flatten-array/.exercism/config.json b/elixir/flatten-array/.exercism/config.json new file mode 100644 index 0000000..88c4178 --- /dev/null +++ b/elixir/flatten-array/.exercism/config.json @@ -0,0 +1,29 @@ +{ + "authors": [ + "bernardoamc" + ], + "contributors": [ + "angelikatyborska", + "Cohen-Carlisle", + "devonestes", + "jwworth", + "lpil", + "neenjaw", + "parkerl", + "sotojuan" + ], + "files": { + "solution": [ + "lib/flatten_array.ex" + ], + "test": [ + "test/flatten_array_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Take a nested list and return a single list with all values except nil/null.", + "source": "Interview Question", + "source_url": "https://reference.wolfram.com/language/ref/Flatten.html" +} diff --git a/elixir/flatten-array/.exercism/metadata.json b/elixir/flatten-array/.exercism/metadata.json new file mode 100644 index 0000000..017e09e --- /dev/null +++ b/elixir/flatten-array/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"flatten-array","id":"639fd9778c30461b89f59d69b0d55de1","url":"https://exercism.org/tracks/elixir/exercises/flatten-array","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/flatten-array/.formatter.exs b/elixir/flatten-array/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/flatten-array/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/flatten-array/.gitignore b/elixir/flatten-array/.gitignore new file mode 100644 index 0000000..fe05d2e --- /dev/null +++ b/elixir/flatten-array/.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"). +flatten_array-*.tar + diff --git a/elixir/flatten-array/HELP.md b/elixir/flatten-array/HELP.md new file mode 100644 index 0000000..f2a7a37 --- /dev/null +++ b/elixir/flatten-array/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/flatten_array.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/flatten-array/README.md b/elixir/flatten-array/README.md new file mode 100644 index 0000000..e4f5520 --- /dev/null +++ b/elixir/flatten-array/README.md @@ -0,0 +1,37 @@ +# Flatten Array + +Welcome to Flatten Array on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Take a nested list and return a single flattened list with all values except nil/null. + +The challenge is to take an arbitrarily-deep nested list-like structure and produce a flattened structure without any nil/null values. + +For example: + +input: [1,[2,3,null,4],[null],5] + +output: [1,2,3,4,5] + +## Source + +### Created by + +- @bernardoamc + +### Contributed to by + +- @angelikatyborska +- @Cohen-Carlisle +- @devonestes +- @jwworth +- @lpil +- @neenjaw +- @parkerl +- @sotojuan + +### Based on + +Interview Question - https://reference.wolfram.com/language/ref/Flatten.html \ No newline at end of file diff --git a/elixir/flatten-array/lib/flatten_array.ex b/elixir/flatten-array/lib/flatten_array.ex new file mode 100644 index 0000000..0c137d0 --- /dev/null +++ b/elixir/flatten-array/lib/flatten_array.ex @@ -0,0 +1,21 @@ +defmodule FlattenArray do + @doc """ + Accept a list and return the list flattened without nil values. + + ## Examples + + iex> FlattenArray.flatten([1, [2], 3, nil]) + [1, 2, 3] + + iex> FlattenArray.flatten([nil, nil]) + [] + + """ + + @spec flatten(list) :: list + def flatten(list) do + list + |> List.flatten() + |> Enum.reject(&is_nil/1) + end +end diff --git a/elixir/flatten-array/mix.exs b/elixir/flatten-array/mix.exs new file mode 100644 index 0000000..11ebc27 --- /dev/null +++ b/elixir/flatten-array/mix.exs @@ -0,0 +1,28 @@ +defmodule FlattenArray.MixProject do + use Mix.Project + + def project do + [ + app: :flatten_array, + 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/flatten-array/test/flatten_array_test.exs b/elixir/flatten-array/test/flatten_array_test.exs new file mode 100644 index 0000000..ce187af --- /dev/null +++ b/elixir/flatten-array/test/flatten_array_test.exs @@ -0,0 +1,58 @@ +defmodule FlattenArrayTest do + use ExUnit.Case + + test "empty" do + assert FlattenArray.flatten([]) == + [] + end + + test "no nesting" do + assert FlattenArray.flatten([0, 1, 2]) == + [0, 1, 2] + end + + test "flattens a nested array" do + assert FlattenArray.flatten([[[]]]) == + [] + end + + test "flattens array with just integers present" do + assert FlattenArray.flatten([1, [2, 3, 4, 5, 6, 7], 8]) == + [1, 2, 3, 4, 5, 6, 7, 8] + end + + test "5 level nesting" do + assert FlattenArray.flatten([0, 2, [[2, 3], 8, 100, 4, [[[50]]]], -2]) == + [0, 2, 2, 3, 8, 100, 4, 50, -2] + end + + test "6 level nesting" do + assert FlattenArray.flatten([1, [2, [[3]], [4, [[5]]], 6, 7], 8]) == + [1, 2, 3, 4, 5, 6, 7, 8] + end + + test "nil values values are omitted from the final result" do + assert FlattenArray.flatten([1, 2, nil]) == + [1, 2] + end + + test "consecutive nil values at the front of the list are omitted from the final result" do + assert FlattenArray.flatten([nil, nil, 3]) == + [3] + end + + test "consecutive nil values in the middle of the list are omitted from the final result" do + assert FlattenArray.flatten([1, nil, nil, 4]) == + [1, 4] + end + + test "6 level nesting with nil values" do + assert FlattenArray.flatten([0, 2, [[2, 3], 8, [[100]], nil, [[nil]]], -2]) == + [0, 2, 2, 3, 8, 100, -2] + end + + test "all values in nested list are null" do + assert FlattenArray.flatten([nil, [[[nil]]], nil, nil, [[nil, nil], nil], nil]) == + [] + end +end diff --git a/elixir/flatten-array/test/test_helper.exs b/elixir/flatten-array/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/flatten-array/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)