mirror of
https://github.com/rjNemo/exercism-elixir
synced 2026-06-06 02:16:48 +00:00
collatz
This commit is contained in:
parent
09255928a2
commit
df544a1666
11 changed files with 273 additions and 6 deletions
26
collatz-conjecture/.exercism/config.json
Normal file
26
collatz-conjecture/.exercism/config.json
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
"blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.",
|
||||
"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"
|
||||
]
|
||||
},
|
||||
"source": "An unsolved problem in mathematics named after mathematician Lothar Collatz",
|
||||
"source_url": "https://en.wikipedia.org/wiki/3x_%2B_1_problem"
|
||||
}
|
||||
1
collatz-conjecture/.exercism/metadata.json
Normal file
1
collatz-conjecture/.exercism/metadata.json
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"track":"elixir","exercise":"collatz-conjecture","id":"3fe9485ac17a4145b1f00dd25b0832e3","url":"https://exercism.org/tracks/elixir/exercises/collatz-conjecture","handle":"rjNemo","is_requester":true,"auto_approve":false}
|
||||
4
collatz-conjecture/.formatter.exs
Normal file
4
collatz-conjecture/.formatter.exs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Used by "mix format"
|
||||
[
|
||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||
]
|
||||
24
collatz-conjecture/.gitignore
vendored
Normal file
24
collatz-conjecture/.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").
|
||||
collatz_conjecture-*.tar
|
||||
|
||||
75
collatz-conjecture/HELP.md
Normal file
75
collatz-conjecture/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/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)
|
||||
- [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).
|
||||
50
collatz-conjecture/README.md
Normal file
50
collatz-conjecture/README.md
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
# 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
|
||||
23
collatz-conjecture/lib/collatz_conjecture.ex
Normal file
23
collatz-conjecture/lib/collatz_conjecture.ex
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
defmodule CollatzConjecture do
|
||||
@moduledoc """
|
||||
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.
|
||||
"""
|
||||
|
||||
@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(1), do: 0
|
||||
def calc(input) when input > 0, do: compute(input, 0)
|
||||
|
||||
defp compute(1, steps), do: steps
|
||||
defp compute(current, steps) when rem(current, 2) == 0, do: compute(div(current, 2), steps + 1)
|
||||
defp compute(current, steps) when rem(current, 2) != 0, do: compute(3 * current + 1, steps + 1)
|
||||
end
|
||||
28
collatz-conjecture/mix.exs
Normal file
28
collatz-conjecture/mix.exs
Normal file
|
|
@ -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
|
||||
39
collatz-conjecture/test/collatz_conjecture_test.exs
Normal file
39
collatz-conjecture/test/collatz_conjecture_test.exs
Normal file
|
|
@ -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
|
||||
2
collatz-conjecture/test/test_helper.exs
Normal file
2
collatz-conjecture/test/test_helper.exs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
ExUnit.start()
|
||||
ExUnit.configure(exclude: :pending, trace: true)
|
||||
|
|
@ -175,12 +175,7 @@ defmodule HighScoreTest do
|
|||
|> HighScore.get_players()
|
||||
|> Enum.sort()
|
||||
|
||||
assert players == [
|
||||
"Chris McCord",
|
||||
"Dave Thomas",
|
||||
"José Valim",
|
||||
"Saša Jurić"
|
||||
]
|
||||
assert players == ["Chris McCord", "Dave Thomas", "José Valim", "Saša Jurić"]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue