Last active
March 21, 2022 08:23
-
-
Save nickangtc/a467cd5de1a8fe08becfeb3f434886d1 to your computer and use it in GitHub Desktop.
Revisions
-
nickangtc revised this gist
Mar 4, 2022 . No changes.There are no files selected for viewing
-
nickangtc revised this gist
Mar 4, 2022 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -21,11 +21,9 @@ def validate_triangle(a, b, c) # Josh's beautiful solution taken from https://stackoverflow.com/a/14445542/6463816 # === def triangle(a, b, c) [a, b, c].permutation do |sides| raise TriangleError unless sides[0] + sides[1] > sides[2] end case [a,b,c].uniq.size when 3; :scalene when 2; :isosceles -
nickangtc created this gist
Mar 4, 2022 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ # exercise is from http://rubykoans.com/ - a great way to learn ruby! #! my not so succinct but works solution def triangle(a, b, c) validate_triangle(a, b, c) if a == b && b == c :equilateral elsif a != b && b != c && c != a :scalene else :isosceles end end def validate_triangle(a, b, c) raise TriangleError, "No such thing as negative or 0-length sides in a triangle" if a <= 0 || b <= 0 || c <= 0 raise TriangleError, "Incomplete triangle because the sides cannot touch and close up" if a + b <= c || a + c <= b || b + c <= a end # === # Josh's beautiful solution taken from https://stackoverflow.com/a/14445542/6463816 # === def triangle(a, b, c) # answers part 2 [a, b, c].permutation do |sides| raise TriangleError unless sides[0] + sides[1] > sides[2] end # answers part 1 case [a,b,c].uniq.size when 3; :scalene when 2; :isosceles when 1; :equilateral end end class TriangleError < StandardError end