From e950cc6d909d03398e93f5d767f55de99aa6dd08 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Sun, 10 Mar 2024 08:53:48 -0400 Subject: [PATCH] strain --- elixir/strain/.exercism/config.json | 29 +++++++ elixir/strain/.exercism/metadata.json | 1 + elixir/strain/.formatter.exs | 4 + elixir/strain/.gitignore | 24 ++++++ elixir/strain/HELP.md | 75 ++++++++++++++++++ elixir/strain/HINTS.md | 5 ++ elixir/strain/README.md | 56 ++++++++++++++ elixir/strain/lib/strain.ex | 22 ++++++ elixir/strain/mix.exs | 28 +++++++ elixir/strain/test/strain_test.exs | 107 ++++++++++++++++++++++++++ elixir/strain/test/test_helper.exs | 2 + 11 files changed, 353 insertions(+) create mode 100644 elixir/strain/.exercism/config.json create mode 100644 elixir/strain/.exercism/metadata.json create mode 100644 elixir/strain/.formatter.exs create mode 100644 elixir/strain/.gitignore create mode 100644 elixir/strain/HELP.md create mode 100644 elixir/strain/HINTS.md create mode 100644 elixir/strain/README.md create mode 100644 elixir/strain/lib/strain.ex create mode 100644 elixir/strain/mix.exs create mode 100644 elixir/strain/test/strain_test.exs create mode 100644 elixir/strain/test/test_helper.exs diff --git a/elixir/strain/.exercism/config.json b/elixir/strain/.exercism/config.json new file mode 100644 index 0000000..b0f97c1 --- /dev/null +++ b/elixir/strain/.exercism/config.json @@ -0,0 +1,29 @@ +{ + "authors": [ + "petehuang" + ], + "contributors": [ + "angelikatyborska", + "Cohen-Carlisle", + "devonestes", + "DoggettCK", + "fxn", + "kytrinyx", + "neenjaw", + "sotojuan" + ], + "files": { + "solution": [ + "lib/strain.ex" + ], + "test": [ + "test/strain_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Implement the `keep` and `discard` operation on collections.", + "source": "Conversation with James Edward Gray II", + "source_url": "http://graysoftinc.com/" +} diff --git a/elixir/strain/.exercism/metadata.json b/elixir/strain/.exercism/metadata.json new file mode 100644 index 0000000..c1c896e --- /dev/null +++ b/elixir/strain/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"strain","id":"b1072621432641a18b1ba8d05e41d7c8","url":"https://exercism.org/tracks/elixir/exercises/strain","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/strain/.formatter.exs b/elixir/strain/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/strain/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/strain/.gitignore b/elixir/strain/.gitignore new file mode 100644 index 0000000..0a4a929 --- /dev/null +++ b/elixir/strain/.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"). +strain-*.tar + diff --git a/elixir/strain/HELP.md b/elixir/strain/HELP.md new file mode 100644 index 0000000..702774e --- /dev/null +++ b/elixir/strain/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/strain.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/strain/HINTS.md b/elixir/strain/HINTS.md new file mode 100644 index 0000000..a2fdb04 --- /dev/null +++ b/elixir/strain/HINTS.md @@ -0,0 +1,5 @@ +# Hints + +## General + +- `apply` will let you pass arguments to a function, as will `fun.(args)`. \ No newline at end of file diff --git a/elixir/strain/README.md b/elixir/strain/README.md new file mode 100644 index 0000000..5455897 --- /dev/null +++ b/elixir/strain/README.md @@ -0,0 +1,56 @@ +# Strain + +Welcome to Strain on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. +If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) + +## Instructions + +Implement the `keep` and `discard` operation on collections. +Given a collection and a predicate on the collection's elements, `keep` returns a new collection containing those elements where the predicate is true, while `discard` returns a new collection containing those elements where the predicate is false. + +For example, given the collection of numbers: + +- 1, 2, 3, 4, 5 + +And the predicate: + +- is the number even? + +Then your keep operation should produce: + +- 2, 4 + +While your discard operation should produce: + +- 1, 3, 5 + +Note that the union of keep and discard is all the elements. + +The functions may be called `keep` and `discard`, or they may need different names in order to not clash with existing functions or concepts in your language. + +## Restrictions + +Keep your hands off that filter/reject/whatchamacallit functionality provided by your standard library! +Solve this one yourself using other basic tools instead. + +## Source + +### Created by + +- @petehuang + +### Contributed to by + +- @angelikatyborska +- @Cohen-Carlisle +- @devonestes +- @DoggettCK +- @fxn +- @kytrinyx +- @neenjaw +- @sotojuan + +### Based on + +Conversation with James Edward Gray II - http://graysoftinc.com/ \ No newline at end of file diff --git a/elixir/strain/lib/strain.ex b/elixir/strain/lib/strain.ex new file mode 100644 index 0000000..ac32991 --- /dev/null +++ b/elixir/strain/lib/strain.ex @@ -0,0 +1,22 @@ +defmodule Strain do + @doc """ + Given a `list` of items and a function `fun`, return the list of items where + `fun` returns true. + + Do not use `Enum.filter`. + """ + @spec keep(list :: list(any), fun :: (any -> boolean)) :: list(any) + def keep([], _fun), do: [] + def keep([head | tail], fun) do + if fun.(head), do: [head | keep(tail, fun)], else: keep(tail, fun) + end + + @doc """ + Given a `list` of items and a function `fun`, return the list of items where + `fun` returns false. + + Do not use `Enum.reject`. + """ + @spec discard(list :: list(any), fun :: (any -> boolean)) :: list(any) + def discard(list, fun), do: keep(list, &!fun.(&1)) +end diff --git a/elixir/strain/mix.exs b/elixir/strain/mix.exs new file mode 100644 index 0000000..b22ab12 --- /dev/null +++ b/elixir/strain/mix.exs @@ -0,0 +1,28 @@ +defmodule Strain.MixProject do + use Mix.Project + + def project do + [ + app: :strain, + 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/strain/test/strain_test.exs b/elixir/strain/test/strain_test.exs new file mode 100644 index 0000000..f24a7c2 --- /dev/null +++ b/elixir/strain/test/strain_test.exs @@ -0,0 +1,107 @@ +defmodule StrainTest do + use ExUnit.Case + + defp is_odd?(n), do: rem(n, 2) == 1 + defp is_even?(n), do: rem(n, 2) == 0 + + describe "keep" do + # @tag :pending + test "on empty list returns empty list" do + assert Strain.keep([], fn _ -> true end) == [] + end + + # @tag :pending + test "keep everything" do + assert Strain.keep([1, 3, 5], fn _ -> true end) == [1, 3, 5] + end + + # @tag :pending + test "keep nothing" do + assert Strain.keep([1, 3, 5], fn _ -> false end) == [] + end + + # @tag :pending + test "keep first and last" do + assert Strain.keep([1, 2, 3], &is_odd?/1) == [1, 3] + end + + # @tag :pending + test "keep neither first nor last" do + assert Strain.keep([1, 2, 3], &is_even?/1) == [2] + end + + # @tag :pending + test "keep strings" do + words = ~w(apple zebra banana zombies cherimoya zelot) + assert Strain.keep(words, &String.starts_with?(&1, "z")) == ~w(zebra zombies zelot) + end + + # @tag :pending + test "keep lists" do + rows = [ + [1, 2, 3], + [5, 5, 5], + [5, 1, 2], + [2, 1, 2], + [1, 5, 2], + [2, 2, 1], + [1, 2, 5] + ] + + assert Strain.keep(rows, fn row -> 5 in row end) == [ + [5, 5, 5], + [5, 1, 2], + [1, 5, 2], + [1, 2, 5] + ] + end + end + + describe "discard" do + # @tag :pending + test "on empty list returns empty list" do + assert Strain.discard([], fn _ -> true end) == [] + end + + # @tag :pending + test "discard everything" do + assert Strain.discard([1, 3, 5], fn _ -> true end) == [] + end + + # @tag :pending + test "discard nothing" do + assert Strain.discard([1, 3, 5], fn _ -> false end) == [1, 3, 5] + end + + # @tag :pending + test "discard first and last" do + assert Strain.discard([1, 2, 3], &is_odd?/1) == [2] + end + + # @tag :pending + test "discard neither first nor last" do + assert Strain.discard([1, 2, 3], &is_even?/1) == [1, 3] + end + + # @tag :pending + test "discard strings" do + words = ~w(apple zebra banana zombies cherimoya zelot) + assert Strain.discard(words, &String.starts_with?(&1, "z")) == ~w(apple banana cherimoya) + end + + # @tag :pending + test "discard arrays" do + rows = [ + [1, 2, 3], + [5, 5, 5], + [5, 1, 2], + [2, 1, 2], + [1, 5, 2], + [2, 2, 1], + [1, 2, 5] + ] + + assert Strain.discard(rows, fn row -> 5 in row end) == [[1, 2, 3], [2, 1, 2], [2, 2, 1]] + end + end +end diff --git a/elixir/strain/test/test_helper.exs b/elixir/strain/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/strain/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)