Skip to content

Instantly share code, notes, and snippets.

@nickangtc
Last active March 21, 2022 08:23
Show Gist options
  • Save nickangtc/a467cd5de1a8fe08becfeb3f434886d1 to your computer and use it in GitHub Desktop.
Save nickangtc/a467cd5de1a8fe08becfeb3f434886d1 to your computer and use it in GitHub Desktop.

Revisions

  1. nickangtc revised this gist Mar 4, 2022. No changes.
  2. nickangtc revised this gist Mar 4, 2022. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions triangle-koan-ruby.rb
    Original 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)
    # 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
  3. nickangtc created this gist Mar 4, 2022.
    37 changes: 37 additions & 0 deletions triangle-koan-ruby.rb
    Original 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