diff --git a/elixir/sublist/.exercism/config.json b/elixir/sublist/.exercism/config.json new file mode 100644 index 0000000..cbab0c7 --- /dev/null +++ b/elixir/sublist/.exercism/config.json @@ -0,0 +1,33 @@ +{ + "authors": [], + "contributors": [ + "angelikatyborska", + "Cohen-Carlisle", + "dalexj", + "devonestes", + "jason-kerney", + "kytrinyx", + "lpil", + "lsimoneau", + "MarcosX", + "neenjaw", + "parkerl", + "pminten", + "rubysolo", + "sotojuan", + "Teapane", + "waiting-for-dev" + ], + "files": { + "solution": [ + "lib/sublist.ex" + ], + "test": [ + "test/sublist_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Write a function to determine if a list is a sublist of another list." +} diff --git a/elixir/sublist/.exercism/metadata.json b/elixir/sublist/.exercism/metadata.json new file mode 100644 index 0000000..4fd32c3 --- /dev/null +++ b/elixir/sublist/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"sublist","id":"45cc4f8c8aab49ed8de0a810e02cf149","url":"https://exercism.org/tracks/elixir/exercises/sublist","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/sublist/.formatter.exs b/elixir/sublist/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/sublist/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/sublist/.gitignore b/elixir/sublist/.gitignore new file mode 100644 index 0000000..a58cf02 --- /dev/null +++ b/elixir/sublist/.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"). +sublist-*.tar + diff --git a/elixir/sublist/HELP.md b/elixir/sublist/HELP.md new file mode 100644 index 0000000..cdda450 --- /dev/null +++ b/elixir/sublist/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/sublist.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/sublist/README.md b/elixir/sublist/README.md new file mode 100644 index 0000000..0f03d1d --- /dev/null +++ b/elixir/sublist/README.md @@ -0,0 +1,55 @@ +# Sublist + +Welcome to Sublist on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Given any two lists `A` and `B`, determine if: + +- List `A` is equal to list `B`; or +- List `A` contains list `B` (`A` is a superlist of `B`); or +- List `A` is contained by list `B` (`A` is a sublist of `B`); or +- None of the above is true, thus lists `A` and `B` are unequal + +Specifically, list `A` is equal to list `B` if both lists have the same values in the same order. +List `A` is a superlist of `B` if `A` contains a sub-sequence of values equal to `B`. +List `A` is a sublist of `B` if `B` contains a sub-sequence of values equal to `A`. + +Examples: + +- If `A = []` and `B = []` (both lists are empty), then `A` and `B` are equal +- If `A = [1, 2, 3]` and `B = []`, then `A` is a superlist of `B` +- If `A = []` and `B = [1, 2, 3]`, then `A` is a sublist of `B` +- If `A = [1, 2, 3]` and `B = [1, 2, 3, 4, 5]`, then `A` is a sublist of `B` +- If `A = [3, 4, 5]` and `B = [1, 2, 3, 4, 5]`, then `A` is a sublist of `B` +- If `A = [3, 4]` and `B = [1, 2, 3, 4, 5]`, then `A` is a sublist of `B` +- If `A = [1, 2, 3]` and `B = [1, 2, 3]`, then `A` and `B` are equal +- If `A = [1, 2, 3, 4, 5]` and `B = [2, 3, 4]`, then `A` is a superlist of `B` +- If `A = [1, 2, 4]` and `B = [1, 2, 3, 4, 5]`, then `A` and `B` are unequal +- If `A = [1, 2, 3]` and `B = [1, 3, 2]`, then `A` and `B` are unequal + +## Slow tests + +One or several of the tests of this exercise have been tagged as `:slow`, because they might take a long time to finish. For this reason, they will not be run on the platform by the automated test runner. If you are solving this exercise directly on the platform in the web editor, you might want to consider downloading this exercise to your machine instead. This will allow you to run all the tests and check the efficiency of your solution. + +## Source + +### Contributed to by + +- @angelikatyborska +- @Cohen-Carlisle +- @dalexj +- @devonestes +- @jason-kerney +- @kytrinyx +- @lpil +- @lsimoneau +- @MarcosX +- @neenjaw +- @parkerl +- @pminten +- @rubysolo +- @sotojuan +- @Teapane +- @waiting-for-dev \ No newline at end of file diff --git a/elixir/sublist/lib/sublist.ex b/elixir/sublist/lib/sublist.ex new file mode 100644 index 0000000..625822f --- /dev/null +++ b/elixir/sublist/lib/sublist.ex @@ -0,0 +1,22 @@ +defmodule Sublist do + @type result :: + :sublist + | :superlist + | :unequal + | :equal + + @doc """ + Returns whether the first list is a sublist or a superlist of the second list + and if not whether it is equal or unequal to the second list. + """ + def compare(a, a), do: :equal + def compare(a, b) when length(a) > length(b), do: (if subset?(b, a), do: :superlist, else: :unequal) + def compare(a, b) when length(a) < length(b), do: (if subset?(a, b), do: :sublist, else: :unequal) + def compare(_, _), do: :unequal + + defp subset?(a, b, index \\ 0) + defp subset?([], _b, _index), do: true + defp subset?(a, b, index) when index + length(a) > length(b), do: false + defp subset?(a, b, index), do: + (if Enum.slice(b, index, length(a)) === a, do: true, else: subset?(a, b, index + 1)) +end diff --git a/elixir/sublist/mix.exs b/elixir/sublist/mix.exs new file mode 100644 index 0000000..4d66de6 --- /dev/null +++ b/elixir/sublist/mix.exs @@ -0,0 +1,28 @@ +defmodule Sublist.MixProject do + use Mix.Project + + def project do + [ + app: :sublist, + 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/sublist/test/sublist_test.exs b/elixir/sublist/test/sublist_test.exs new file mode 100644 index 0000000..3e1dba2 --- /dev/null +++ b/elixir/sublist/test/sublist_test.exs @@ -0,0 +1,113 @@ +defmodule SublistTest do + use ExUnit.Case + + test "empty equals empty" do + assert Sublist.compare([], []) == :equal + end + + test "empty list within non empty list" do + assert Sublist.compare([], [1, 2, 3]) == :sublist + end + + test "non empty list contains empty list" do + assert Sublist.compare([1, 2, 3], []) == :superlist + end + + test "list equals itself" do + assert Sublist.compare([1, 2, 3], [1, 2, 3]) == :equal + end + + test "different lists" do + assert Sublist.compare([1, 2, 3], [2, 3, 4]) == :unequal + end + + test "comparing massive equal lists" do + l = Enum.to_list(1..1_000_000) + assert Sublist.compare(l, l) == :equal + end + + test "false start" do + assert Sublist.compare([1, 2, 5], [0, 1, 2, 3, 1, 2, 5, 6]) == :sublist + end + + test "consecutive" do + assert Sublist.compare([1, 1, 2], [0, 1, 1, 1, 2, 1, 2]) == :sublist + end + + test "sublists at start" do + assert Sublist.compare([0, 1, 2], [0, 1, 2, 3, 4, 5]) == :sublist + end + + test "sublists in middle" do + assert Sublist.compare([2, 3, 4], [0, 1, 2, 3, 4, 5]) == :sublist + end + + test "sublists at end" do + assert Sublist.compare([3, 4, 5], [0, 1, 2, 3, 4, 5]) == :sublist + end + + test "partially matching sublist at end" do + assert Sublist.compare([1, 1, 2], [1, 1, 1, 2]) == :sublist + end + + test "sublist early in huge list" do + assert Sublist.compare([3, 4, 5], Enum.to_list(1..1_000_000)) == :sublist + end + + @tag :slow + test "huge sublist not in huge list" do + assert Sublist.compare(Enum.to_list(10..1_000_001), Enum.to_list(1..1_000_000)) == :unequal + end + + test "at start of superlist" do + assert Sublist.compare([0, 1, 2, 3, 4, 5], [0, 1, 2]) == :superlist + end + + test "in middle of superlist" do + assert Sublist.compare([0, 1, 2, 3, 4, 5], [2, 3]) == :superlist + end + + test "at end of superlist" do + assert Sublist.compare([0, 1, 2, 3, 4, 5], [3, 4, 5]) == :superlist + end + + test "at end of partially matching superlist" do + assert Sublist.compare([1, 1, 1, 2], [1, 1, 2]) == :superlist + end + + test "superlist early in huge list" do + assert Sublist.compare(Enum.to_list(1..1_000_000), [3, 4, 5]) == :superlist + end + + test "first list missing element from second list" do + assert Sublist.compare([1, 3], [1, 2, 3]) == :unequal + end + + test "second list missing element from first list" do + assert Sublist.compare([1, 2, 3], [1, 3]) == :unequal + end + + test "first list missing additional digits from second list" do + assert Sublist.compare([1, 2], [1, 22]) == :unequal + end + + test "order matters to a list" do + assert Sublist.compare([1, 2, 3], [3, 2, 1]) == :unequal + end + + test "same digits but different numbers" do + assert Sublist.compare([1, 0, 1], [10, 1]) == :unequal + end + + test "strict equality needed" do + assert Sublist.compare([1], [1.0, 2]) == :unequal + end + + test "recurring values sublist" do + assert Sublist.compare([1, 2, 1, 2, 3], [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]) == :sublist + end + + test "recurring values unequal" do + assert Sublist.compare([1, 2, 1, 2, 3], [1, 2, 3, 1, 2, 3, 2, 3, 2, 1]) == :unequal + end +end diff --git a/elixir/sublist/test/test_helper.exs b/elixir/sublist/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/sublist/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)