Avatar of lujanfernaud

lujanfernaud's solution

to Triangle in the Ruby Track

Published at Mar 16 2019 · 1 comment
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.


For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby triangle_test.rb

To include color from the command line:

ruby -r minitest/pride triangle_test.rb

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

require 'minitest/autorun'
require_relative 'triangle'

# Common test data version: 1.2.0 55f89ca
class TriangleTest < Minitest::Test
  def test_triangle_is_equilateral_if_all_sides_are_equal
    # skip
    triangle = Triangle.new([2, 2, 2])
    assert triangle.equilateral?, "Expected 'true', triangle [2, 2, 2] is equilateral."
  end

  def test_triangle_is_not_equilateral_if_any_side_is_unequal
    skip
    triangle = Triangle.new([2, 3, 2])
    refute triangle.equilateral?, "Expected 'false', triangle [2, 3, 2] is not equilateral."
  end

  def test_triangle_is_not_equilateral_if_no_sides_are_equal
    skip
    triangle = Triangle.new([5, 4, 6])
    refute triangle.equilateral?, "Expected 'false', triangle [5, 4, 6] is not equilateral."
  end

  def test_all_zero_sides_are_illegal_so_the_triangle_is_not_equilateral
    skip
    triangle = Triangle.new([0, 0, 0])
    refute triangle.equilateral?, "Expected 'false', triangle [0, 0, 0] is not equilateral."
  end

  def test_equilateral_triangle_sides_may_be_floats
    skip
    triangle = Triangle.new([0.5, 0.5, 0.5])
    assert triangle.equilateral?, "Expected 'true', triangle [0.5, 0.5, 0.5] is equilateral."
  end

  def test_triangle_is_isosceles_if_last_two_sides_are_equal
    skip
    triangle = Triangle.new([3, 4, 4])
    assert triangle.isosceles?, "Expected 'true', triangle [3, 4, 4] is isosceles."
  end

  def test_triangle_is_isosceles_if_first_two_sides_are_equal
    skip
    triangle = Triangle.new([4, 4, 3])
    assert triangle.isosceles?, "Expected 'true', triangle [4, 4, 3] is isosceles."
  end

  def test_triangle_is_isosceles_if_first_and_last_sides_are_equal
    skip
    triangle = Triangle.new([4, 3, 4])
    assert triangle.isosceles?, "Expected 'true', triangle [4, 3, 4] is isosceles."
  end

  def test_equilateral_triangles_are_also_isosceles
    skip
    triangle = Triangle.new([4, 4, 4])
    assert triangle.isosceles?, "Expected 'true', triangle [4, 4, 4] is isosceles."
  end

  def test_triangle_is_not_isosceles_if_no_sides_are_equal
    skip
    triangle = Triangle.new([2, 3, 4])
    refute triangle.isosceles?, "Expected 'false', triangle [2, 3, 4] is not isosceles."
  end

  def test_sides_that_violate_triangle_inequality_are_not_isosceles_even_if_two_are_equal_1
    skip
    triangle = Triangle.new([1, 1, 3])
    refute triangle.isosceles?, "Expected 'false', triangle [1, 1, 3] is not isosceles."
  end

  def test_sides_that_violate_triangle_inequality_are_not_isosceles_even_if_two_are_equal_2
    skip
    triangle = Triangle.new([1, 3, 1])
    refute triangle.isosceles?, "Expected 'false', triangle [1, 3, 1] is not isosceles."
  end

  def test_sides_that_violate_triangle_inequality_are_not_isosceles_even_if_two_are_equal_3
    skip
    triangle = Triangle.new([3, 1, 1])
    refute triangle.isosceles?, "Expected 'false', triangle [3, 1, 1] is not isosceles."
  end

  def test_isosceles_triangle_sides_may_be_floats
    skip
    triangle = Triangle.new([0.5, 0.4, 0.5])
    assert triangle.isosceles?, "Expected 'true', triangle [0.5, 0.4, 0.5] is isosceles."
  end

  def test_triangle_is_scalene_if_no_sides_are_equal
    skip
    triangle = Triangle.new([5, 4, 6])
    assert triangle.scalene?, "Expected 'true', triangle [5, 4, 6] is scalene."
  end

  def test_triangle_is_not_scalene_if_all_sides_are_equal
    skip
    triangle = Triangle.new([4, 4, 4])
    refute triangle.scalene?, "Expected 'false', triangle [4, 4, 4] is not scalene."
  end

  def test_triangle_is_not_scalene_if_two_sides_are_equal
    skip
    triangle = Triangle.new([4, 4, 3])
    refute triangle.scalene?, "Expected 'false', triangle [4, 4, 3] is not scalene."
  end

  def test_sides_that_violate_triangle_inequality_are_not_scalene_even_if_they_are_all_different
    skip
    triangle = Triangle.new([7, 3, 2])
    refute triangle.scalene?, "Expected 'false', triangle [7, 3, 2] is not scalene."
  end

  def test_scalene_triangle_sides_may_be_floats
    skip
    triangle = Triangle.new([0.5, 0.4, 0.6])
    assert triangle.scalene?, "Expected 'true', triangle [0.5, 0.4, 0.6] is scalene."
  end
end
class Triangle
  def initialize(triangle)
    @triangle = triangle
  end

  def equilateral?
    valid? && sides_with_same_length == 1
  end

  def isosceles?
    valid? && (sides_with_same_length == 1 || sides_with_same_length == 2)
  end

  def scalene?
    valid? && !equilateral? && !isosceles?
  end

  private

  def valid?
    !unique_side_lengths.include?(0) && valid_proportions?
  end

  def unique_side_lengths
    @unique_side_lengths ||= triangle.uniq
  end

  def valid_proportions?
    sorted_side_lengths = triangle.max(3)
    longer_side = sorted_side_lengths.shift

    longer_side <= sorted_side_lengths.sum
  end

  def sides_with_same_length
    @sides_with_same_length ||= unique_side_lengths.size
  end

  attr_reader :triangle
end

Community comments

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

In the end I didn't get to use it, but just want to leave a note here about the array set intersection operator (Array#&).

[0, 1, 2] & [0, 1] #=> [0, 1]
(edited 65 days ago)

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?