mirror of
https://github.com/rjNemo/exercism-elixir
synced 2026-06-06 02:16:48 +00:00
strain
This commit is contained in:
parent
d1a80440a8
commit
3b7a535a6e
11 changed files with 337 additions and 0 deletions
29
strain/.exercism/config.json
Normal file
29
strain/.exercism/config.json
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
{
|
||||||
|
"blurb": "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.",
|
||||||
|
"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"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"source": "Conversation with James Edward Gray II",
|
||||||
|
"source_url": "https://twitter.com/jeg2"
|
||||||
|
}
|
||||||
1
strain/.exercism/metadata.json
Normal file
1
strain/.exercism/metadata.json
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
{"track":"elixir","exercise":"strain","id":"b3af0b1256ea40e1a4721bfcd7a862c5","url":"https://exercism.org/tracks/elixir/exercises/strain","handle":"rjNemo","is_requester":true,"auto_approve":false}
|
||||||
4
strain/.formatter.exs
Normal file
4
strain/.formatter.exs
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Used by "mix format"
|
||||||
|
[
|
||||||
|
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||||
|
]
|
||||||
24
strain/.gitignore
vendored
Normal file
24
strain/.gitignore
vendored
Normal file
|
|
@ -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
|
||||||
|
|
||||||
75
strain/HELP.md
Normal file
75
strain/HELP.md
Normal file
|
|
@ -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)
|
||||||
|
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
|
||||||
|
- 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.
|
||||||
|
If you can't find what you're looking for in the documentation, feel free to ask help in the Exercism's BEAM [gitter channel](https://gitter.im/exercism/xerlang).
|
||||||
3
strain/HINTS.md
Normal file
3
strain/HINTS.md
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
# Hints
|
||||||
|
|
||||||
|
- `apply` will let you pass arguments to a function, as will `fun.(args)`.
|
||||||
61
strain/README.md
Normal file
61
strain/README.md
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
# 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 - https://twitter.com/jeg2
|
||||||
27
strain/lib/strain.ex
Normal file
27
strain/lib/strain.ex
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
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([], _), do: []
|
||||||
|
|
||||||
|
def keep([h | tail], fun) do
|
||||||
|
if fun.(h) == true do
|
||||||
|
[h | keep(tail, fun)]
|
||||||
|
else
|
||||||
|
keep(tail, fun)
|
||||||
|
end
|
||||||
|
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
|
||||||
28
strain/mix.exs
Normal file
28
strain/mix.exs
Normal file
|
|
@ -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
|
||||||
83
strain/test/strain_test.exs
Normal file
83
strain/test/strain_test.exs
Normal file
|
|
@ -0,0 +1,83 @@
|
||||||
|
defmodule StrainTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
|
||||||
|
defp is_odd?(n), do: rem(n, 2) == 1
|
||||||
|
defp is_even?(n), do: rem(n, 2) == 0
|
||||||
|
defp noop(_), do: true
|
||||||
|
|
||||||
|
# @tag :pending
|
||||||
|
test "empty keep" do
|
||||||
|
assert Strain.keep([], &noop/1) == []
|
||||||
|
end
|
||||||
|
|
||||||
|
test "keep everything" do
|
||||||
|
assert Strain.keep([1, 2, 3], fn e -> e < 10 end) == [1, 2, 3]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "keep first and last" do
|
||||||
|
assert Strain.keep([1, 2, 3], &is_odd?/1) == [1, 3]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "keep neither first nor last" do
|
||||||
|
assert Strain.keep([1, 2, 3, 4, 5], &is_even?/1) == [2, 4]
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
test "keep 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.keep(rows, fn row -> 5 in row end) == [
|
||||||
|
[5, 5, 5],
|
||||||
|
[5, 1, 2],
|
||||||
|
[1, 5, 2],
|
||||||
|
[1, 2, 5]
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "empty discard" do
|
||||||
|
assert Strain.discard([], &noop/1) == []
|
||||||
|
end
|
||||||
|
|
||||||
|
test "discard nothing" do
|
||||||
|
assert Strain.discard([1, 2, 3], fn e -> e > 10 end) == [1, 2, 3]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "discard first and last" do
|
||||||
|
assert Strain.discard([1, 2, 3], &is_odd?/1) == [2]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "discard neither first nor last" do
|
||||||
|
assert Strain.discard([1, 2, 3, 4, 5], &is_even?/1) == [1, 3, 5]
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
2
strain/test/test_helper.exs
Normal file
2
strain/test/test_helper.exs
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
ExUnit.start()
|
||||||
|
ExUnit.configure(exclude: :pending, trace: true)
|
||||||
Loading…
Reference in a new issue