Ruby Koan 151 raising exceptions

后端 未结 30 1583
孤独总比滥情好
孤独总比滥情好 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 14:58

    No Need to change the TriangleError code for either challenge. You just need to check for invalid triangles and raise the error if the triangle isn't.

    def triangle(a, b, c)
      if a==0 && b==0 && c==0
        raise TriangleError, "This isn't a triangle"
      end
      if a <0 or b < 0 or c <0
        raise TriangleError, "Negative length - thats not right"
      end
      if a + b <= c  or a + c <= b or  b + c <= a
        raise TriangleError, "One length can't be more (or the same as) than the other two added together.  If it was the same, the whole thing would be a line.  If more, it wouldn't reach. "
      end    
      # WRITE THIS CODE
      if a == b and b == c 
        return :equilateral    
      end      
      if (a==b or b == c or a == c) 
        return :isosceles
      end 
      :scalene      
    end
    
    0 讨论(0)
  • 2021-01-31 14:59

    I don't think I see this one here, yet.

    I believe all the illegal triangle conditions imply that the longest side can't be more than half the total. i.e:

    def triangle(a, b, c)
    
      fail TriangleError, "Illegal triangle: [#{a}, #{b}, #{c}]" if
        [a, b, c].max >= (a + b + c) / 2.0
    
      return :equilateral if a == b and b == c
      return :isosceles if a == b or b == c or a == c
      return :scalene
    
    end
    
    0 讨论(0)
  • 2021-01-31 14:59

    There are some absolutely brilliant people on StackOverflow...I'm reminded of that every time I visit :D Just to contribute to the conversation, here's the solution I came up with:

    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
    
        equal_sides = 0
        equal_sides +=1 if a == b
        equal_sides +=1 if a == c
        equal_sides +=1 if b == c
    
        # Note that equal_sides will never be 2.  If it hits 2 
        # of the conditions, it will have to hit all 3 by the law
        # of associativity
        return [:scalene, :isosceles, nil, :equilateral][equal_sides] 
    end 
    
    0 讨论(0)
  • 2021-01-31 15:04

    This one did take some brain time. But here's my solution

    def triangle(a, b, c)
      # WRITE THIS CODE
      raise TriangleError, "All sides must be positive number" if a <= 0 || b <= 0 || c <= 0
      raise TriangleError, "Impossible triangle" if ( a + b + c - ( 2 *  [a,b,c].max ) <= 0  )
    
      if(a == b && a == c)
          :equilateral
      elsif (a == b || b == c || a == c)
          :isosceles
      else
        :scalene
      end
    end
    
    0 讨论(0)
  • 2021-01-31 15:04

    Here's my solution... honestly I can't think of a more concise and readable one!

    def triangle(a, b, c)
      raise TriangleError unless a > 0 && b > 0 && c > 0
      raise TriangleError if a == b && a + b <= c
      raise TriangleError if a == c && a + c <= b
      return :equilateral if a == b && b == c
      return :isosceles   if a == b || b == c || c == a
      :scalene
    end
    
    0 讨论(0)
  • 2021-01-31 15:05

    For the Koan about_triangle_project_2.rb there's no need to change TriangleError class. Insert this code before your triangle algorithm to pass all tests:

    if ((a<=0 || b<=0 || c<=0))
        raise TriangleError
    end
    
    if ((a+b<=c) || (b+c<=a) || (a+c<=b))
        raise TriangleError
    end
    
    0 讨论(0)
提交回复
热议问题