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.
In order to run the tests, issue the following command from the exercise directory:
For running the tests provided, rebar3
is used as it is the official build and
dependency management tool for erlang now. Please refer to the tracks installation
instructions on how to do that.
In order to run the tests, you can issue the following command from the exercise directory.
$ rebar3 eunit
For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.
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.
%% Based on canonical data version 1.2.0
%% https://github.com/exercism/problem-specifications/raw/master/exercises/triangle/canonical-data.json
%% This file is automatically generated from the exercises canonical data.
-module(triangle_tests).
-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").
'1_equilateral_true_if_all_sides_are_equal_test'() ->
?assertMatch(equilateral, triangle:kind(2, 2, 2)).
'2_equilateral_all_zero_sides_are_illegal_so_the_triangle_is_not_equilateral_test'() ->
?assertMatch({error,
"all side lengths must be positive"},
triangle:kind(0, 0, 0)).
'3_equilateral_sides_may_be_floats_test'() ->
?assertMatch(equilateral,
triangle:kind(5.0e-1, 5.0e-1, 5.0e-1)).
'4_isosceles_true_if_last_two_sides_are_equal_test'() ->
?assertMatch(isosceles, triangle:kind(3, 4, 4)).
'5_isosceles_true_if_first_two_sides_are_equal_test'() ->
?assertMatch(isosceles, triangle:kind(4, 4, 3)).
'6_isosceles_true_if_first_and_last_sides_are_equal_test'() ->
?assertMatch(isosceles, triangle:kind(4, 3, 4)).
'7_isosceles_sides_that_violate_triangle_inequality_are_not_isosceles_even_if_two_are_equal_1_test'() ->
?assertMatch({error,
"side lengths violate triangle inequality"},
triangle:kind(1, 1, 3)).
'8_isosceles_sides_that_violate_triangle_inequality_are_not_isosceles_even_if_two_are_equal_2_test'() ->
?assertMatch({error,
"side lengths violate triangle inequality"},
triangle:kind(1, 3, 1)).
'9_isosceles_sides_that_violate_triangle_inequality_are_not_isosceles_even_if_two_are_equal_3_test'() ->
?assertMatch({error,
"side lengths violate triangle inequality"},
triangle:kind(3, 1, 1)).
'10_isosceles_sides_may_be_floats_test'() ->
?assertMatch(isosceles,
triangle:kind(5.0e-1, 4.0e-1, 5.0e-1)).
'11_scalene_true_if_no_sides_are_equal_test'() ->
?assertMatch(scalene, triangle:kind(5, 4, 6)).
'12_scalene_sides_that_violate_triangle_inequality_are_not_scalene_even_if_they_are_all_different_test'() ->
?assertMatch({error,
"side lengths violate triangle inequality"},
triangle:kind(7, 3, 2)).
'13_scalene_sides_may_be_floats_test'() ->
?assertMatch(scalene,
triangle:kind(5.0e-1, 4.0e-1,
5.99999999999999977796e-1)).
-module(triangle).
-export([kind/3]).
kind(A, B, C) when (A =< 0) or (B =< 0) or (C =< 0) ->
{error, "all side lengths must be positive"};
kind(A, B, C) when (A + B < C) or (A + C < B) or (B + C < A) ->
{error, "side lengths violate triangle inequality"};
kind(A, A, A) -> equilateral;
kind(A, A, _) -> isosceles;
kind(_, A, A) -> isosceles;
kind(A, _, A) -> isosceles;
kind(_, _, _) -> scalene.
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