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

#31
You don't need to modify the Exception. Something like this should work;


def triangle(*args)
args.sort!
raise TriangleError if args[0] + args[1] <= args[2] || args[0] <= 0
[nil, :equilateral, :isosceles, :scalene][args.uniq.length]
end
Reply

#32
I wanted a method that parsed all arguments effectively instead of relying on the order given in the test assertions.

def triangle(a, b, c)
# WRITE THIS CODE
[a,b,c].permutation { |p|
if p[0] + p[1] <= p[2]
raise TriangleError, "Two sides of a triangle must be greater than the remaining side."
elsif p.count { |x| x <= 0} > 0
raise TriangleError, "A triangle cannot have sides of zero or less length."
end
}

if [a,b,c].uniq.count == 1
return :equilateral
elsif [a,b,c].uniq.count == 2
return :isosceles
elsif [a,b,c].uniq.count == 3
return :scalene
end
end

Hopefully this helps other realize there is more than one way to skin a cat.
Reply

#33
I ended up with this code:

<!-- language: ruby -->

def triangle(a, b, c)
raise TriangleError, "impossible triangle" if [a,b,c].min <= 0
x, y, z = [a,b,c].sort
raise TriangleError, "no two sides can be < than the third" if x + y <= z

if a == b && b == c # && a == c # XXX: last check implied by previous 2
:equilateral
elsif a == b || b == c || c == a
:isosceles
else
:scalene
end
end

I don't like the second condition/raise, but I'm unsure how to improve it further.
Reply

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

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

#36
1. A triangle should not have any sides of length 0. If it does, it's either a line segment or a point, depending on how many sides are 0.
2. Negative length doesn't make sense.
3. Any two sides of a triangle should add up to more than the third side.
4. See 3, and focus on the "more".

You shouldn't need to change the TriangleError code, AFAICS. Looks like your syntax is just a little wacky. Try changing

raise new.TriangleError

to

raise TriangleError, "why the exception happened"

Also, you should be testing the values (and throwing exceptions) before you do anything with them. Move the exception stuff to the beginning of the function.
Reply

#37
You have to check that the new created triangle don't break the "Triangle inequality". You can ensure this by this little formula.

if !((a-b).abs < c && c < a + b)
raise TriangleError
end

When you get the Error:

<TriangleError> exception expected but none was thrown.

Your code is probably throwing an exception while creating a regular triangle in this file. about_triangle_project.rb
Reply

#38
I like Cory's answer. But I wonder if there's any reason or anything to gain by having four tests, when you could have two:


raise TriangleError, "Sides must by numbers greater than zero" if (a <= 0) || (b <= 0) || (c <= 0)
raise TriangleError, "No two sides can add to be less than or equal to the other side" if (a+b <= c) || (a+c <= b) || (b+c <= a)
Reply

#39
Here is what I wrote and it all worked fine.

def triangle(a, b, c)
# WRITE THIS CODE
raise TriangleError, "Sides have to be greater than zero" if (a == 0) | (b == 0) | (c == 0)
raise TriangleError, "Sides have to be a postive number" if (a < 0) | (b < 0) | (c < 0)
raise TriangleError, "Two sides can never be less than the sum of one side" if ((a + b) < c) | ((a + c) < b) | ((b + c) < a)
raise TriangleError, "Two sides can never be equal one side" if ((a + b) == c) | ((a + c) == b) | ((b + c) == a)
return :equilateral if (a == b) & (a == c) & (b == c)
return :isosceles if (a == b) | (a == c) | (b == c)
return :scalene

end

# Error class used in part 2. No need to change this code.
class TriangleError < StandardError
end
Reply

#40
You definately do not update the TriangleError class - I am stuck on 152 myself. I think I need to use the pythag theorem here.

def triangle(a, b, c)
# WRITE THIS CODE

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

# The sum of two sides should be less than the other side
if((a+b < c) || (a+c < b) || (b+c < a))
raise TriangleError
end
if a==b && b==c
return :equilateral
end
if (a==b && a!=c) || (a==c && a!=b) || (b==c && b!=a)
return :isosceles
end
if(a!=b && a!=c && b!=c)
return :scalene
end


end

# Error class used in part 2. No need to change this code.
class TriangleError < StandardError
end

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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