strain
This commit is contained in:
parent
c625ecedd6
commit
e950cc6d90
|
@ -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/"
|
||||||
|
}
|
|
@ -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}
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Used by "mix format"
|
||||||
|
[
|
||||||
|
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||||
|
]
|
|
@ -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
|
||||||
|
|
|
@ -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/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.
|
|
@ -0,0 +1,5 @@
|
||||||
|
# Hints
|
||||||
|
|
||||||
|
## General
|
||||||
|
|
||||||
|
- `apply` will let you pass arguments to a function, as will `fun.(args)`.
|
|
@ -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/
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -0,0 +1,2 @@
|
||||||
|
ExUnit.start()
|
||||||
|
ExUnit.configure(exclude: :pending, trace: true)
|
Loading…
Reference in New Issue