Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 653 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ruby Koan 151 raising exceptions

#21
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
Reply

#22
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
Reply

#23
def triangle(a, b, c)
[a, b, c].permutation do |sides|
raise TriangleError unless sides[0] + sides[1] > sides[2]
end
case [a,b,c].uniq.size
when 3; :scalene
when 2; :isosceles
when 1; :equilateral
end
end
Reply

#24
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
Reply

#25
Ended up doing this:

def triangle(a, b, c)
a, b, c = [a, b, c].sort
raise TriangleError if a <= 0 || a + b <= c
[nil, :equilateral, :isosceles, :scalene][[a, b, c].uniq.size]
end

Thanks to commenters here :)
Reply

#26
In fact in the following code the condition a <= 0 is redundant. a + b will always be less than c if a < 0 and we know that b < c

raise TriangleError if a <= 0 || a + b <= c
Reply

#27
This is what I ended up with. It is sort of a combination of a few of the above examples with my own unique take on the triangle inequality exception (it considers the degenerate case as well). Seems to work.

def triangle(a, b, c)

raise TriangleError if [a,b,c].min <= 0
raise TriangleError if [a,b,c].sort.reverse.reduce(:-) >= 0

return :equilateral if a == b && b == c
return :isosceles if a == b || a == c || b == c
return :scalene

end
Reply

#28
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
Reply

#29
Here is my version... :-)

<!-- language: lang-rb -->

def triangle(a, b, c)

if a <= 0 || b <= 0 || c <= 0
raise TriangleError
end

if a + b <= c || a + c <= b || b + c <= a
raise TriangleError
end

return :equilateral if a == b && b == c
return :isosceles if a == b || a == c || b == c
return :scalene if a != b && a != c && b != c

end
Reply

#30
You could also try to instance the exception with:

raise TriangleError.new("All sides must be greater than 0") if a * b * c <= 0
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through