triangle
This commit is contained in:
parent
15ab62c4f1
commit
9425fd5368
|
@ -0,0 +1,34 @@
|
|||
{
|
||||
"authors": [
|
||||
"rubysolo"
|
||||
],
|
||||
"contributors": [
|
||||
"andrewsardone",
|
||||
"angelikatyborska",
|
||||
"Cohen-Carlisle",
|
||||
"dalexj",
|
||||
"devonestes",
|
||||
"jinyeow",
|
||||
"lpil",
|
||||
"neenjaw",
|
||||
"parkerl",
|
||||
"ryanzidago",
|
||||
"sotojuan",
|
||||
"Teapane",
|
||||
"waiting-for-dev"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"lib/triangle.ex"
|
||||
],
|
||||
"test": [
|
||||
"test/triangle_test.exs"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.ex"
|
||||
]
|
||||
},
|
||||
"blurb": "Determine if a triangle is equilateral, isosceles, or scalene.",
|
||||
"source": "The Ruby Koans triangle project, parts 1 & 2",
|
||||
"source_url": "https://web.archive.org/web/20220831105330/http://rubykoans.com"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"elixir","exercise":"triangle","id":"4438c1b9c97b462a976fc55154458a8f","url":"https://exercism.org/tracks/elixir/exercises/triangle","handle":"negrienko","is_requester":true,"auto_approve":false}
|
|
@ -0,0 +1,4 @@
|
|||
# Used by "mix format"
|
||||
[
|
||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||
]
|
|
@ -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").
|
||||
triangle-*.tar
|
||||
|
|
@ -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/triangle.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.
|
|
@ -0,0 +1,60 @@
|
|||
# Triangle
|
||||
|
||||
Welcome to Triangle on Exercism's Elixir Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Determine if a triangle is equilateral, isosceles, or scalene.
|
||||
|
||||
An _equilateral_ triangle has all three sides the same length.
|
||||
|
||||
An _isosceles_ triangle has at least two sides the same length.
|
||||
(It is sometimes specified as having exactly two sides the same length, but for the purposes of this exercise we'll say at least two.)
|
||||
|
||||
A _scalene_ triangle has all sides of different lengths.
|
||||
|
||||
## Note
|
||||
|
||||
For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side.
|
||||
|
||||
In equations:
|
||||
|
||||
Let `a`, `b`, and `c` be sides of the triangle.
|
||||
Then all three of the following expressions must be true:
|
||||
|
||||
```text
|
||||
a + b ≥ c
|
||||
b + c ≥ a
|
||||
a + c ≥ b
|
||||
```
|
||||
|
||||
See [Triangle Inequality][triangle-inequality]
|
||||
|
||||
[triangle-inequality]: https://en.wikipedia.org/wiki/Triangle_inequality
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @rubysolo
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @andrewsardone
|
||||
- @angelikatyborska
|
||||
- @Cohen-Carlisle
|
||||
- @dalexj
|
||||
- @devonestes
|
||||
- @jinyeow
|
||||
- @lpil
|
||||
- @neenjaw
|
||||
- @parkerl
|
||||
- @ryanzidago
|
||||
- @sotojuan
|
||||
- @Teapane
|
||||
- @waiting-for-dev
|
||||
|
||||
### Based on
|
||||
|
||||
The Ruby Koans triangle project, parts 1 & 2 - https://web.archive.org/web/20220831105330/http://rubykoans.com
|
|
@ -0,0 +1,13 @@
|
|||
defmodule Triangle do
|
||||
@type kind :: :equilateral | :isosceles | :scalene
|
||||
|
||||
@doc """
|
||||
Return the kind of triangle of a triangle with 'a', 'b' and 'c' as lengths.
|
||||
"""
|
||||
@spec kind(number, number, number) :: {:ok, kind} | {:error, String.t()}
|
||||
def kind(a, b, c) when a <= 0 or b <= 0 or c <= 0, do: {:error, "all side lengths must be positive"}
|
||||
def kind(a, b, c) when a + b <= c or a + c <= b or b + c <= a, do: {:error, "side lengths violate triangle inequality"}
|
||||
def kind(a, a, a), do: {:ok, :equilateral}
|
||||
def kind(a, b, c) when a == b or b == c or c == a, do: {:ok, :isosceles}
|
||||
def kind(_a, _b, _c), do: {:ok, :scalene}
|
||||
end
|
|
@ -0,0 +1,28 @@
|
|||
defmodule Triangle.MixProject do
|
||||
use Mix.Project
|
||||
|
||||
def project do
|
||||
[
|
||||
app: :triangle,
|
||||
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
|
|
@ -0,0 +1,2 @@
|
|||
ExUnit.start()
|
||||
ExUnit.configure(exclude: :pending, trace: true)
|
|
@ -0,0 +1,75 @@
|
|||
defmodule TriangleTest do
|
||||
use ExUnit.Case
|
||||
|
||||
test "equilateral triangles have equal sides" do
|
||||
assert Triangle.kind(2, 2, 2) == {:ok, :equilateral}
|
||||
end
|
||||
|
||||
test "larger equilateral triangles also have equal sides" do
|
||||
assert Triangle.kind(10, 10, 10) == {:ok, :equilateral}
|
||||
end
|
||||
|
||||
test "equilateral triangles can have float sides" do
|
||||
assert Triangle.kind(0.5, 0.5, 0.5) == {:ok, :equilateral}
|
||||
end
|
||||
|
||||
test "isosceles triangles have last two sides equal" do
|
||||
assert Triangle.kind(3, 4, 4) == {:ok, :isosceles}
|
||||
end
|
||||
|
||||
test "isosceles triangles have first and last sides equal" do
|
||||
assert Triangle.kind(4, 3, 4) == {:ok, :isosceles}
|
||||
end
|
||||
|
||||
test "isosceles triangles have two first sides equal" do
|
||||
assert Triangle.kind(4, 4, 3) == {:ok, :isosceles}
|
||||
end
|
||||
|
||||
test "isosceles triangles have in fact exactly two sides equal" do
|
||||
assert Triangle.kind(10, 10, 2) == {:ok, :isosceles}
|
||||
end
|
||||
|
||||
test "isosceles triangles can have float sides" do
|
||||
assert Triangle.kind(0.5, 0.4, 0.5) == {:ok, :isosceles}
|
||||
end
|
||||
|
||||
test "scalene triangles have no equal sides" do
|
||||
assert Triangle.kind(3, 4, 5) == {:ok, :scalene}
|
||||
end
|
||||
|
||||
test "scalene triangles have no equal sides at a larger scale too" do
|
||||
assert Triangle.kind(10, 11, 12) == {:ok, :scalene}
|
||||
end
|
||||
|
||||
test "scalene triangles have no equal sides in descending order either" do
|
||||
assert Triangle.kind(5, 4, 2) == {:ok, :scalene}
|
||||
end
|
||||
|
||||
test "scalene triangles can have float sides" do
|
||||
assert Triangle.kind(0.4, 0.6, 0.3) == {:ok, :scalene}
|
||||
end
|
||||
|
||||
test "triangles with no size are illegal" do
|
||||
assert Triangle.kind(0, 0, 0) == {:error, "all side lengths must be positive"}
|
||||
end
|
||||
|
||||
test "triangles with negative sides are illegal" do
|
||||
assert Triangle.kind(3, 4, -5) == {:error, "all side lengths must be positive"}
|
||||
end
|
||||
|
||||
test "first triangle inequality violation" do
|
||||
assert Triangle.kind(1, 1, 3) == {:error, "side lengths violate triangle inequality"}
|
||||
end
|
||||
|
||||
test "second triangle inequality violation" do
|
||||
assert Triangle.kind(1, 3, 1) == {:error, "side lengths violate triangle inequality"}
|
||||
end
|
||||
|
||||
test "third triangle inequality violation" do
|
||||
assert Triangle.kind(3, 1, 1) == {:error, "side lengths violate triangle inequality"}
|
||||
end
|
||||
|
||||
test "another triangle inequality violation" do
|
||||
assert Triangle.kind(7, 3, 2) == {:error, "side lengths violate triangle inequality"}
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue