From b62fcbc5ea07537b43695c296c197391b432539a Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Wed, 26 Jun 2024 23:06:52 -0400 Subject: [PATCH] =?UTF-8?q?=D1=81ollatz-=D1=81onjecture?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../collatz-conjecture/.exercism/config.json | 26 +++++++ .../.exercism/metadata.json | 1 + elixir/collatz-conjecture/.formatter.exs | 4 + elixir/collatz-conjecture/.gitignore | 24 ++++++ elixir/collatz-conjecture/HELP.md | 75 +++++++++++++++++++ elixir/collatz-conjecture/README.md | 52 +++++++++++++ .../lib/collatz_conjecture.ex | 15 ++++ elixir/collatz-conjecture/mix.exs | 28 +++++++ .../test/collatz_conjecture_test.exs | 39 ++++++++++ .../collatz-conjecture/test/test_helper.exs | 2 + 10 files changed, 266 insertions(+) create mode 100644 elixir/collatz-conjecture/.exercism/config.json create mode 100644 elixir/collatz-conjecture/.exercism/metadata.json create mode 100644 elixir/collatz-conjecture/.formatter.exs create mode 100644 elixir/collatz-conjecture/.gitignore create mode 100644 elixir/collatz-conjecture/HELP.md create mode 100644 elixir/collatz-conjecture/README.md create mode 100644 elixir/collatz-conjecture/lib/collatz_conjecture.ex create mode 100644 elixir/collatz-conjecture/mix.exs create mode 100644 elixir/collatz-conjecture/test/collatz_conjecture_test.exs create mode 100644 elixir/collatz-conjecture/test/test_helper.exs diff --git a/elixir/collatz-conjecture/.exercism/config.json b/elixir/collatz-conjecture/.exercism/config.json new file mode 100644 index 0000000..c55238e --- /dev/null +++ b/elixir/collatz-conjecture/.exercism/config.json @@ -0,0 +1,26 @@ +{ + "authors": [ + "Tuxified" + ], + "contributors": [ + "angelikatyborska", + "Cohen-Carlisle", + "devonestes", + "neenjaw", + "sotojuan" + ], + "files": { + "solution": [ + "lib/collatz_conjecture.ex" + ], + "test": [ + "test/collatz_conjecture_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.", + "source": "An unsolved problem in mathematics named after mathematician Lothar Collatz", + "source_url": "https://en.wikipedia.org/wiki/3x_%2B_1_problem" +} diff --git a/elixir/collatz-conjecture/.exercism/metadata.json b/elixir/collatz-conjecture/.exercism/metadata.json new file mode 100644 index 0000000..62cd92e --- /dev/null +++ b/elixir/collatz-conjecture/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"collatz-conjecture","id":"ccd0e87e9b8944838d567e25ffb84d23","url":"https://exercism.org/tracks/elixir/exercises/collatz-conjecture","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/collatz-conjecture/.formatter.exs b/elixir/collatz-conjecture/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/collatz-conjecture/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/collatz-conjecture/.gitignore b/elixir/collatz-conjecture/.gitignore new file mode 100644 index 0000000..a47ad81 --- /dev/null +++ b/elixir/collatz-conjecture/.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"). +collatz_conjecture-*.tar + diff --git a/elixir/collatz-conjecture/HELP.md b/elixir/collatz-conjecture/HELP.md new file mode 100644 index 0000000..f941abc --- /dev/null +++ b/elixir/collatz-conjecture/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/collatz_conjecture.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/collatz-conjecture/README.md b/elixir/collatz-conjecture/README.md new file mode 100644 index 0000000..caea261 --- /dev/null +++ b/elixir/collatz-conjecture/README.md @@ -0,0 +1,52 @@ +# Collatz Conjecture + +Welcome to Collatz Conjecture on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +The Collatz Conjecture or 3x+1 problem can be summarized as follows: + +Take any positive integer n. +If n is even, divide n by 2 to get n / 2. +If n is odd, multiply n by 3 and add 1 to get 3n + 1. +Repeat the process indefinitely. +The conjecture states that no matter which number you start with, you will always reach 1 eventually. + +Given a number n, return the number of steps required to reach 1. + +## Examples + +Starting with n = 12, the steps would be as follows: + +0. 12 +1. 6 +2. 3 +3. 10 +4. 5 +5. 16 +6. 8 +7. 4 +8. 2 +9. 1 + +Resulting in 9 steps. +So for input n = 12, the return value would be 9. + +## Source + +### Created by + +- @Tuxified + +### Contributed to by + +- @angelikatyborska +- @Cohen-Carlisle +- @devonestes +- @neenjaw +- @sotojuan + +### Based on + +An unsolved problem in mathematics named after mathematician Lothar Collatz - https://en.wikipedia.org/wiki/3x_%2B_1_problem \ No newline at end of file diff --git a/elixir/collatz-conjecture/lib/collatz_conjecture.ex b/elixir/collatz-conjecture/lib/collatz_conjecture.ex new file mode 100644 index 0000000..a478731 --- /dev/null +++ b/elixir/collatz-conjecture/lib/collatz_conjecture.ex @@ -0,0 +1,15 @@ +defmodule CollatzConjecture do + @doc """ + calc/1 takes an integer and returns the number of steps required to get the + number to 1 when following the rules: + - if number is odd, multiply with 3 and add 1 + - if number is even, divide by 2 + """ + @spec calc(input :: pos_integer()) :: non_neg_integer() + def calc(input) when is_integer(input) and input > 0, do: collatz(input) + + defp collatz(input, steps \\ 0) + defp collatz(1, steps), do: steps + defp collatz(input, steps) when rem(input, 2) == 0, do: collatz(div(input, 2), steps + 1) + defp collatz(input, steps), do: collatz(input * 3 + 1, steps + 1) +end diff --git a/elixir/collatz-conjecture/mix.exs b/elixir/collatz-conjecture/mix.exs new file mode 100644 index 0000000..4d9e251 --- /dev/null +++ b/elixir/collatz-conjecture/mix.exs @@ -0,0 +1,28 @@ +defmodule CollatzConjecture.MixProject do + use Mix.Project + + def project do + [ + app: :collatz_conjecture, + 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/collatz-conjecture/test/collatz_conjecture_test.exs b/elixir/collatz-conjecture/test/collatz_conjecture_test.exs new file mode 100644 index 0000000..0c7cedc --- /dev/null +++ b/elixir/collatz-conjecture/test/collatz_conjecture_test.exs @@ -0,0 +1,39 @@ +defmodule CollatzConjectureTest do + use ExUnit.Case + + test "zero steps for one" do + assert CollatzConjecture.calc(1) == 0 + end + + test "zero is an error" do + assert_raise FunctionClauseError, fn -> CollatzConjecture.calc(0) end + end + + test "divide if even" do + assert CollatzConjecture.calc(16) == 4 + end + + test "even and odd steps" do + assert CollatzConjecture.calc(12) == 9 + end + + test "Large number of even and odd steps" do + assert CollatzConjecture.calc(1_000_000) == 152 + end + + test "start with odd step" do + assert CollatzConjecture.calc(21) == 7 + end + + test "more steps than starting number" do + assert CollatzConjecture.calc(7) == 16 + end + + test "negative value is an error " do + assert_raise FunctionClauseError, fn -> CollatzConjecture.calc(-15) end + end + + test "string as input value is an error " do + assert_raise FunctionClauseError, fn -> CollatzConjecture.calc("fubar") end + end +end diff --git a/elixir/collatz-conjecture/test/test_helper.exs b/elixir/collatz-conjecture/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/collatz-conjecture/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)