# PercyGrunwald's solution

## to Triangle in the Elixir Track

Published at Jan 08 2019 · 0 comments
Instructions
Test suite
Solution

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. See Triangle Inequality.

## Dig Deeper

The case where the sum of the lengths of two sides equals that of the third is known as a degenerate triangle - it has zero area and looks like a single line. Feel free to add your own code/tests to check for degenerate triangles.

## Running tests

Execute the tests with:

\$ elixir triangle_test.exs

### Pending tests

In the test suites, all but the first test have been 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:

# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

## Source

The Ruby Koans triangle project, parts 1 & 2 http://rubykoans.com

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### triangle_test.exs

if !System.get_env("EXERCISM_TEST_EXAMPLES") do
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule TriangleTest do
use ExUnit.Case

# @tag :pending
test "equilateral triangles have equal sides" do
assert Triangle.kind(2, 2, 2) == {:ok, :equilateral}
end

@tag :pending
test "larger equilateral triangles also have equal sides" do
assert Triangle.kind(10, 10, 10) == {:ok, :equilateral}
end

@tag :pending
test "isosceles triangles have last two sides equal" do
assert Triangle.kind(3, 4, 4) == {:ok, :isosceles}
end

@tag :pending
test "isosceles triangles have first and last sides equal" do
assert Triangle.kind(4, 3, 4) == {:ok, :isosceles}
end

@tag :pending
test "isosceles triangles have two first sides equal" do
assert Triangle.kind(4, 4, 3) == {:ok, :isosceles}
end

@tag :pending
test "isosceles triangles have in fact exactly two sides equal" do
assert Triangle.kind(10, 10, 2) == {:ok, :isosceles}
end

@tag :pending
test "scalene triangles have no equal sides" do
assert Triangle.kind(3, 4, 5) == {:ok, :scalene}
end

@tag :pending
test "scalene triangles have no equal sides at a larger scale too" do
assert Triangle.kind(10, 11, 12) == {:ok, :scalene}
end

@tag :pending
test "scalene triangles have no equal sides in descending order either" do
assert Triangle.kind(5, 4, 2) == {:ok, :scalene}
end

@tag :pending
test "very small triangles are legal" do
assert Triangle.kind(0.4, 0.6, 0.3) == {:ok, :scalene}
end

@tag :pending
test "triangles with no size are illegal" do
assert Triangle.kind(0, 0, 0) == {:error, "all side lengths must be positive"}
end

@tag :pending
test "triangles with negative sides are illegal" do
assert Triangle.kind(3, 4, -5) == {:error, "all side lengths must be positive"}
end

@tag :pending
test "triangles violating triangle inequality are illegal" do
assert Triangle.kind(1, 1, 3) == {:error, "side lengths violate triangle inequality"}
end

@tag :pending
test "triangles violating triangle inequality are illegal 2" do
assert Triangle.kind(2, 4, 2) == {:error, "side lengths violate triangle inequality"}
end

@tag :pending
test "triangles violating triangle inequality are illegal 3" do
assert Triangle.kind(7, 3, 2) == {:error, "side lengths violate triangle inequality"}
end
end
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) do
cond do
not all_side_lengths_positive?(a, b, c) ->
{:error, "all side lengths must be positive"}

not triangle_meets_triangle_inequality?(a, b, c) ->
{:error, "side lengths violate triangle inequality"}

is_equilateral?(a, b, c) ->
{:ok, :equilateral}

is_isosceles?(a, b, c) ->
{:ok, :isosceles}

is_scalene?(a, b, c) ->
{:ok, :scalene}

true ->
{:error, "Not a valid triangle"}
end
end

defp all_side_lengths_positive?(a, b, c) do
a > 0 and b > 0 and c > 0
end

defp triangle_meets_triangle_inequality?(a, b, c) do
b + c > a and a + c > b and a + b > c
end

defp is_equilateral?(a, b, c) do
a == b and b == c
end

defp is_isosceles?(a, b, c) do
a == b or a == c or b == c
end

defp is_scalene?(a, b, c) do
a != b and a != c and b != c
end
end