Created
June 14, 2012 03:01
-
-
Save istro/2927818 to your computer and use it in GitHub Desktop.
Method to find the type of triangle by the length of three sides
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The problem: | |
# Write a triangle method that accepts three numbers as arguments. The method should return a print | |
# whether the three lengths form an equilateral, isosceles, or scalene triangle. | |
# | |
# We were just learning to define methods, and having learned if/else statements and touched on | |
# ternary statements, so we just wanted to make it as concise as we could with our limited knowledge :) | |
# | |
def triangle(side1, side2, side3) | |
if (side1 == side2 && side1 == side3) || (side1 + side2 <= side3 || side3 + side2 <= side1 || side1 + side3 <= side2) | |
(side1 == side2 && side1 == side3) ? puts "equilateral!" : puts "invalid!" | |
else | |
(side1 == side2 || side1 == side3 || side3 == side2) ? puts "isosceles!" : puts "scalene!" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment