Ruby Koan 151 raising exceptions

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

    Here is my elegant answer, with a lot of help from the comments above

    def triangle(a, b, c)
    
       test_tri = [a,b,c]
    
       if test_tri.min <=0
         raise TriangleError
       end
    
       test_tri.sort!
    
       if test_tri[0]+ test_tri[1] <= test_tri[2]
         raise TriangleError
       end
    
       if a == b and b == c
         :equilateral
       elsif a != b and b != c and a != c 
         :scalene   
       else
         :isosceles     
       end
    end
    

提交回复
热议问题