Ruby Koan 151 raising exceptions

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

    Rules:

    1. size must be > 0

    2. Total of any 2 sides, must be bigger that the 3rd

    Code:

    raise TriangleError if ( [a,b,c].any? {|x| (x <= 0)} ) or ( ((a+b)<=c) or ((b+c)<=a) or ((a+c)<=b))
    [:equilateral, :isosceles, :scalene].fetch([a,b,c].uniq.size - 1)
    

提交回复
热议问题