Ruby Koan 151 raising exceptions

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

提交回复
热议问题