# paulfioravanti's solution

## to Triangle in the Ruby 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.

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
IS_EQUILATERAL = ->(unique_sides) { unique_sides == 1 }
private_constant :IS_EQUILATERAL
IS_ISOCELES = ->(unique_sides) { unique_sides < 3 }
private_constant :IS_ISOCELES
IS_SCALENE = ->(unique_sides) { unique_sides == 3 }
private_constant :IS_SCALENE

def initialize(sides)
@sides = sides.sort
end

def equilateral?
legal_triangle?(&IS_EQUILATERAL)
end

def isosceles?
legal_triangle?(&IS_ISOCELES)
end

def scalene?
legal_triangle?(&IS_SCALENE)
end

private

def legal_triangle?(&condition)
sides.all?(&:positive?) &&
side_lengths_all_legal? &&
sides.uniq.size.yield_self(&condition)
end

def side_lengths_all_legal?
sides.yield_self do |(*first_two_sides, third_side)|
first_two_sides.sum >= third_side
end
end
end``````