ruby - triangle
This commit is contained in:
parent
4c861d0858
commit
c5e159ed19
53
ruby/triangle/README.md
Normal file
53
ruby/triangle/README.md
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
# Triangle
|
||||||
|
|
||||||
|
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](https://en.wikipedia.org/wiki/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](http://exercism.io/languages/ruby/resources).
|
||||||
|
|
||||||
|
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](http://rubykoans.com)
|
||||||
|
|
||||||
|
## Submitting Incomplete Solutions
|
||||||
|
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
24
ruby/triangle/triangle.rb
Normal file
24
ruby/triangle/triangle.rb
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# Triangle class
|
||||||
|
class Triangle
|
||||||
|
def initialize(sides)
|
||||||
|
@sides = sides.sort
|
||||||
|
end
|
||||||
|
|
||||||
|
def equilateral?
|
||||||
|
@sides.uniq.length == 1 && triangle_equality?
|
||||||
|
end
|
||||||
|
|
||||||
|
def isosceles?
|
||||||
|
@sides.uniq.length < 3 && triangle_equality?
|
||||||
|
end
|
||||||
|
|
||||||
|
def scalene?
|
||||||
|
@sides.uniq.length == 3 && triangle_equality?
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def triangle_equality?
|
||||||
|
@sides.min > 0 && @sides[0] + @sides[1] >= @sides[2]
|
||||||
|
end
|
||||||
|
end
|
119
ruby/triangle/triangle_test.rb
Normal file
119
ruby/triangle/triangle_test.rb
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
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
|
Loading…
x
Reference in New Issue
Block a user