Ruby Koan 151 raising exceptions

后端 未结 30 1558
孤独总比滥情好
孤独总比滥情好 2021-01-31 14:34

I\'m going through the ruby koans, I\'m on 151 and I just hit a brick wall.

Here is the koan:

# You need to write the triangle method in the file \'trian         


        
相关标签:
30条回答
  • 2021-01-31 15:06

    Here is what I wrote and it all worked fine.

    def triangle(a, b, c)
      # WRITE THIS CODE
      raise TriangleError, "Sides have to be greater than zero" if (a == 0) | (b == 0) | (c == 0)
      raise TriangleError, "Sides have to be a postive number" if (a < 0) | (b < 0) | (c < 0)
      raise TriangleError, "Two sides can never be less than the sum of one side" if ((a + b) < c) | ((a + c) < b) | ((b + c) < a)
      raise TriangleError, "Two sides can never be equal one side" if ((a + b) ==  c) | ((a + c) ==  b) | ((b + c) ==  a)
      return :equilateral if (a == b) & (a == c) & (b == c)
      return :isosceles if (a == b) | (a == c) | (b == c)
      return :scalene
    
    end
    
    # Error class used in part 2.  No need to change this code.
    class TriangleError < StandardError
    end
    
    0 讨论(0)
  • 2021-01-31 15:06
      #(1)Any zero or -ve values
      if [a,b,c].any? { |side_length| side_length <= 0 }
        raise TriangleError
      end
    
      #(2)Any side of a triangle must be less than the sum of the other two sides
      # a <  b+c, b <  a+c  and c <  a+b  a  valid   triangle
      # a >= b+c, b >= a+c  and c >= a+b  an invalid triangle
    
      total_of_side_lengths = [a,b,c].inject {|total,x| total += x}
    
      if [a,b,c].any? { |side_length| side_length >= (total_of_side_lengths - side_length)}
        raise TriangleError
      end
    
    0 讨论(0)
  • 2021-01-31 15:07

    After try to understand what I must to do with koan 151, I got it with the first posts, and get lot fun to check everyone solution :) ... here is the mine:

    def triangle(a, b, c)
      array = [a, b, c].sort
      raise TriangleError if array.min <= 0 || array[0]+array[1] <= array[2]
      array.uniq!
      array.length == 1 ? :equilateral: array.length == 2 ? :isosceles : :scalene
    end
    

    Koan is a very interesting way to learn Ruby

    0 讨论(0)
  • 2021-01-31 15:07

    You could also try to instance the exception with:

    raise TriangleError.new("All sides must be greater than 0") if a * b * c <= 0
    
    0 讨论(0)
  • 2021-01-31 15:08

    You forgot the case when a,b, or c are negative:

    def triangle(a, b, c)
      raise TriangleError if [a,b,c].min <= 0
      x, y, z = [a,b,c].sort
      raise TriangleError if x + y <= z
      [:equilateral,:isosceles,:scalene].fetch([a,b,c].uniq.size - 1)
    end
    
    0 讨论(0)
  • 2021-01-31 15:08

    I ended up with this code:

    def triangle(a, b, c)
        raise TriangleError, "impossible triangle" if [a,b,c].min <= 0
        x, y, z = [a,b,c].sort
        raise TriangleError, "no two sides can be < than the third" if x + y <= z
    
        if a == b && b == c # && a == c # XXX: last check implied by previous 2
            :equilateral
        elsif a == b || b == c || c == a
            :isosceles
        else
            :scalene
        end
    end 
    

    I don't like the second condition/raise, but I'm unsure how to improve it further.

    0 讨论(0)
提交回复
热议问题