From 95c9a93dec3e47bdd2967065831d7470e66710d5 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Wed, 30 Apr 2025 10:25:00 -0400 Subject: [PATCH] Add Dominoes exercise for Elixir track --- elixir/dominoes/.exercism/config.json | 24 ++++++++ elixir/dominoes/.exercism/metadata.json | 1 + elixir/dominoes/.formatter.exs | 4 ++ elixir/dominoes/.gitignore | 24 ++++++++ elixir/dominoes/HELP.md | 75 +++++++++++++++++++++++++ elixir/dominoes/README.md | 48 ++++++++++++++++ elixir/dominoes/lib/dominoes.ex | 22 ++++++++ elixir/dominoes/mix.exs | 29 ++++++++++ elixir/dominoes/test/dominoes_test.exs | 74 ++++++++++++++++++++++++ elixir/dominoes/test/test_helper.exs | 2 + 10 files changed, 303 insertions(+) create mode 100644 elixir/dominoes/.exercism/config.json create mode 100644 elixir/dominoes/.exercism/metadata.json create mode 100644 elixir/dominoes/.formatter.exs create mode 100644 elixir/dominoes/.gitignore create mode 100644 elixir/dominoes/HELP.md create mode 100644 elixir/dominoes/README.md create mode 100644 elixir/dominoes/lib/dominoes.ex create mode 100644 elixir/dominoes/mix.exs create mode 100644 elixir/dominoes/test/dominoes_test.exs create mode 100644 elixir/dominoes/test/test_helper.exs diff --git a/elixir/dominoes/.exercism/config.json b/elixir/dominoes/.exercism/config.json new file mode 100644 index 0000000..e8fce5d --- /dev/null +++ b/elixir/dominoes/.exercism/config.json @@ -0,0 +1,24 @@ +{ + "authors": [ + "Tuxified" + ], + "contributors": [ + "angelikatyborska", + "Cohen-Carlisle", + "devonestes", + "neenjaw", + "sotojuan" + ], + "files": { + "solution": [ + "lib/dominoes.ex" + ], + "test": [ + "test/dominoes_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Make a chain of dominoes." +} diff --git a/elixir/dominoes/.exercism/metadata.json b/elixir/dominoes/.exercism/metadata.json new file mode 100644 index 0000000..8811bd2 --- /dev/null +++ b/elixir/dominoes/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"dominoes","id":"c3029f8eea4743b88999e49c85863daa","url":"https://exercism.org/tracks/elixir/exercises/dominoes","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/dominoes/.formatter.exs b/elixir/dominoes/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/dominoes/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/dominoes/.gitignore b/elixir/dominoes/.gitignore new file mode 100644 index 0000000..1396353 --- /dev/null +++ b/elixir/dominoes/.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"). +dominoes-*.tar + diff --git a/elixir/dominoes/HELP.md b/elixir/dominoes/HELP.md new file mode 100644 index 0000000..4322300 --- /dev/null +++ b/elixir/dominoes/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/dominoes.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/dominoes/README.md b/elixir/dominoes/README.md new file mode 100644 index 0000000..7374180 --- /dev/null +++ b/elixir/dominoes/README.md @@ -0,0 +1,48 @@ +# Dominoes + +Welcome to Dominoes on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Introduction + +In Toyland, the trains are always busy delivering treasures across the city, from shiny marbles to rare building blocks. +The tracks they run on are made of colorful domino-shaped pieces, each marked with two numbers. +For the trains to move, the dominoes must form a perfect chain where the numbers match. + +Today, an urgent delivery of rare toys is on hold. +You've been handed a set of track pieces to inspect. +If they can form a continuous chain, the train will be on its way, bringing smiles across Toyland. +If not, the set will be discarded, and another will be tried. + +The toys are counting on you to solve this puzzle. +Will the dominoes connect the tracks and send the train rolling, or will the set be left behind? + +## Instructions + +Make a chain of dominoes. + +Compute a way to order a given set of domino stones so that they form a correct domino chain. +In the chain, the dots on one half of a stone must match the dots on the neighboring half of an adjacent stone. +Additionally, the dots on the halves of the stones without neighbors (the first and last stone) must match each other. + +For example given the stones `[2|1]`, `[2|3]` and `[1|3]` you should compute something +like `[1|2] [2|3] [3|1]` or `[3|2] [2|1] [1|3]` or `[1|3] [3|2] [2|1]` etc, where the first and last numbers are the same. + +For stones `[1|2]`, `[4|1]` and `[2|3]` the resulting chain is not valid: `[4|1] [1|2] [2|3]`'s first and last numbers are not the same. +4 != 3 + +Some test cases may use duplicate stones in a chain solution, assume that multiple Domino sets are being used. + +## Source + +### Created by + +- @Tuxified + +### Contributed to by + +- @angelikatyborska +- @Cohen-Carlisle +- @devonestes +- @neenjaw +- @sotojuan \ No newline at end of file diff --git a/elixir/dominoes/lib/dominoes.ex b/elixir/dominoes/lib/dominoes.ex new file mode 100644 index 0000000..6d47485 --- /dev/null +++ b/elixir/dominoes/lib/dominoes.ex @@ -0,0 +1,22 @@ +defmodule Dominoes do + @type domino :: {1..6, 1..6} + + @doc """ + chain?/1 takes a list of domino stones and returns boolean indicating if it's + possible to make a full chain + """ + @spec chain?(dominoes :: [domino]) :: boolean + def chain?([]), do: true + def chain?([{first, second}]) when first == second, do: true + + def chain?([{first, second} | dominoes]) do + Enum.any?( + dominoes, + fn + {^first, x} = domino -> chain?([{second, x} | List.delete(dominoes, domino)]) + {x, ^first} = domino -> chain?([{second, x} | List.delete(dominoes, domino)]) + _ -> false + end + ) + end +end diff --git a/elixir/dominoes/mix.exs b/elixir/dominoes/mix.exs new file mode 100644 index 0000000..3a7bdd8 --- /dev/null +++ b/elixir/dominoes/mix.exs @@ -0,0 +1,29 @@ +defmodule Dominoes.MixProject do + use Mix.Project + + def project do + [ + app: :dominoes, + 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 + [ + {:lettuce, "~> 0.3.0", only: ~w(dev)a} + # {: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/dominoes/test/dominoes_test.exs b/elixir/dominoes/test/dominoes_test.exs new file mode 100644 index 0000000..31e7dee --- /dev/null +++ b/elixir/dominoes/test/dominoes_test.exs @@ -0,0 +1,74 @@ +defmodule DominoesTest do + use ExUnit.Case + + test "empty input = empty output" do + assert Dominoes.chain?([]) == true + end + + test "singleton input = singleton output" do + assert Dominoes.chain?([{1, 1}]) == true + end + + test "singleton that can't be chained" do + assert Dominoes.chain?([{1, 2}]) == false + end + + test "three elements" do + assert Dominoes.chain?([{1, 2}, {3, 1}, {2, 3}]) == true + end + + test "can reverse dominoes" do + assert Dominoes.chain?([{1, 2}, {1, 3}, {2, 3}]) == true + end + + test "can't be chained" do + assert Dominoes.chain?([{1, 2}, {4, 1}, {2, 3}]) == false + end + + test "disconnected - double loop" do + assert Dominoes.chain?([{1, 2}, {2, 1}, {3, 4}, {4, 3}]) == false + end + + test "disconnected - single isolated" do + assert Dominoes.chain?([{1, 2}, {2, 3}, {3, 1}, {4, 4}]) == false + end + + test "need backtrack" do + # a variation in which we have to turn but no duplicates + assert Dominoes.chain?([{1, 2}, {2, 3}, {3, 1}, {2, 4}, {2, 4}]) == true + end + + test "separate loops" do + assert Dominoes.chain?([{1, 2}, {2, 3}, {3, 1}, {1, 1}, {2, 2}, {3, 3}]) == true + end + + test "nine elements" do + assert Dominoes.chain?([ + {1, 2}, + {5, 3}, + {3, 1}, + {1, 2}, + {2, 4}, + {1, 6}, + {2, 3}, + {3, 4}, + {5, 6} + ]) == true + end + + test "separate three-domino loops" do + refute Dominoes.chain?([{1, 2}, {2, 3}, {3, 1}, {4, 5}, {5, 6}, {6, 4}]) + end + + test "disconnected - simple" do + refute Dominoes.chain?([{1, 1}, {2, 2}]) + end + + test "first and last not matching" do + assert Dominoes.chain?([{1, 2}, {2, 3}, {3, 4}]) == false + end + + test "wrong starting order" do + assert Dominoes.chain?([{2, 1}, {2, 3}, {3, 1}]) == true + end +end diff --git a/elixir/dominoes/test/test_helper.exs b/elixir/dominoes/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/dominoes/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)