Avatar of shmibs

shmibs's solution

to Triangle in the Elixir Track

Published at Jul 13 2018 · 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

For more detailed information about the Elixir track, please see the help page.

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
  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
    cond do
      a <= 0 or b <= 0 or c <= 0 ->
        { :error, "all side lengths must be positive" }
      a + b <=c or b + c <= a or a + c <= b ->
        { :error, "side lengths violate triangle inequality" }
      a == b and b == c ->
        { :ok, :equilateral }
      a == b or b == c or a == c ->
        { :ok, :isosceles }
      true ->
        { :ok, :scalene }
    end
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?