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.
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.
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.
Execute the tests with:
$ elixir triangle_test.exs
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.
The Ruby Koans triangle project, parts 1 & 2 http://rubykoans.com
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
if !System.get_env("EXERCISM_TEST_EXAMPLES") do
Code.load_file("triangle.exs", __DIR__)
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
case validate_triangle(a, b, c) do
:ok ->
cond do
equilateral?(a, b, c) -> {:ok, :equilateral}
isosceles?(a, b, c) -> {:ok, :isosceles}
true -> {:ok, :scalene}
end
{:error, error} ->
{:error, error}
end
end
defp validate_triangle(a, b, c) do
cond do
!all_sizes_positive?(a, b, c) ->
{:error, "all side lengths must be positive"}
!triangle_inequality?(a, b, c) ->
{:error, "side lengths violate triangle inequality"}
true ->
:ok
end
end
defp all_sizes_positive?(a, b, c) do
a > 0 && b > 0 && c > 0
end
defp triangle_inequality?(a, b, c) do
a + b > c && b + c > a && a + c > b
end
defp equilateral?(a, b, c) do
a == b && a == c
end
defp isosceles?(a, b, c) do
a == b || b == c || a == c
end
end
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments