 # thekeele's solution

## to Triangle in the Elixir Track

Published at Nov 03 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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:

``````\$ mix test
``````

### 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.

### test_helper.exs

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

### triangle_test.exs

``````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

@spec kind(number, number, number) :: {:ok, kind} | {:error, String.t()}
def kind(x, y, z) when x <= 0 or y <= 0 or z <= 0,
do: {:error, "all side lengths must be positive"}

def kind(x, y, z) when (x + y <= z) or (y + z <= x) or (z + x <= y),
do: {:error, "side lengths violate triangle inequality"}

def kind(x, y, z) when x === y and y === z,
do: {:ok, :equilateral}

def kind(x, y, z) when x === y or y === z or z === x,
do: {:ok, :isosceles}

def kind(x, y, z) when x !== y and y !== z,
do: {:ok, :scalene}
end``````