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

#1
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 'triangle.rb'
require 'triangle.rb'

class AboutTriangleProject2 < EdgeCase::Koan
# The first assignment did not talk about how to handle errors.
# Let's handle that part now.
def test_illegal_triangles_throw_exceptions
assert_raise(TriangleError) do triangle(0, 0, 0) end
assert_raise(TriangleError) do triangle(3, 4, -5) end
assert_raise(TriangleError) do triangle(1, 1, 3) end
assert_raise(TriangleError) do triangle(2, 4, 2) end
end
end

Then in triangle.rb we have:

def triangle(a, b, c)
# WRITE THIS CODE
if a==b && a==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
if a==0 && b==0 && c==0
raise new.TriangleError
end



end

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

end

I am beyond confused - any help at all would be much appreciated!

EDIT: To complete this koan, I need to put something in the TriangleError class - but I have no idea what

UPDATE: Here is what the koan karma thing is saying:

<TriangleError> exception expected but none was thrown.
Reply

#2
I would like to see a reason for each TriangleError exception, so each failure case has a nice text message for the reason it failed. Also, each line or block of code is handling one workflow only for legibility. The three return cases are very clearly read and understood. I want to make it easy on myself or someone else who looks at this code later.

```
def triangle(a, b, c)

t = [a, b, c].sort

raise TriangleError, "side cannot be 0" if t.any?(0)

t.each do |x|
raise TriangleError, "side cannot be negative" if x < 0
end

raise TriangleError, "two sides added cannot be less than or equal to one side" if t[0] + t[1] <= t[2]

return :equilateral if (a == b) && (b == c)
return :scalene if (a != b) && (a != c) && (b != c)
return :isosceles
end
```
Reply

#3
The triangle.rb file need these update to work.

def triangle(a, b, c)
# If length is Less Then or equal to 0 the triangle not possible
# it raise error. If the length of any two sum is less or equal to other-
# Again the triangle not possible.
raise TriangleError if (a <= 0 || b <= 0 || c <= 0) || (a+b <= c || b+c <= a || c+a <= b)

# If the size of each equal its Equilateral Triangle.
return :equilateral if (a == b && a == c)

# If any two lenght is equal than it Isosceles Triangle.
return :isosceles if (a == b || b == c || a == c)

# If each lenght is different than its Scalene Triangle.
return :scalene if (a != b) && (b != c) && (a != c)
end

class TriangleError < StandardError
end
Reply

#4
Leon wins on fancy elegance, Benji for his knowledge of the Array API. Here's my brute elegant answer:

def triangle(a, b, c)
[a, b, c].each { | side | raise TriangleError, "Sides must be positive" unless side > 0 }
raise TriangleError, "Two sides can never be less than or equal to third side" if ((a + b) <= c) | ((a + c) <= b) | ((b + c) <= a)

return :equilateral if (a == b) && (b == c)
return :isosceles if (a == b) || (b == c) || (a == c)
return :scalene
end
Reply

#5
**SIMPLEST SOLUTION!**

def triangle(a, b, c)
#This would check for if the numbers are negative or 0.
if a <= 0 || b <= 0 || c <= 0
raise TriangleError
end
#This would check if the two sides of a triangle are greater than the remaining side.
if a + b <= c || a + c <= b || b + c <= a
raise TriangleError
end

if a == b && b = c
:equilateral
elsif a == b || a == c || b == c
:isosceles
else
:scalene
end
end


Thanks to all the beautiful people for their answer I have checked them all and I would say no need to change the TriangleError code. we need to check for invalid triangles and raise the error if the triangle is not.
Reply

#6
My solution, I think it's one of the more readable ones:

def triangle(a, b, c)
a, b, c = [a, b, c].sort
if a <= 0 or c >= a + b
raise TriangleError
end
case [a, b, c].uniq.length
when 1
:equilateral
when 2
:isosceles
when 3
:scalene
end
end

Reply

#7
You can use this formula and validate all cases:

- a + b > c
- a + c > b
- b + c > a

and you code like:

<!-- language: lang-ruby -->

def triangle(a, b, c)
# Validate the triangle
raise TriangleError, "The triangle is not valid" unless (a + b) > c && (a + c) > b && (b + c) > a

if a === b && b === c
:equilateral
elsif a === b || a === c || b === c
:isosceles
else
:scalene
end
end
Reply

#8
# your previous triangle method should appear here



class TriangleError < StandardError
end

def triangle(x,y,z)
if(x>=y+z||y>=x+z||z>=x+y)
raise TriangleError,"impossible triangle"
elsif(x==0&&y==0&&z==0)||(x<0||y<0||z<0)
raise TriangleError,"length cannot be zero or negative"
elsif(x==y&&x==z)
:equilateral
elsif(x==y||y==z||x==z)
:isosceles
else
:scalene
end
end
Reply

#9

This is my solution:-

def triangle(a, b, c)
if a <= 0 || b <= 0 || c <= 0 || a + b <= c || a + c <= b || b + c <= a
raise TriangleError
elsif (a == b) &&( a==c) && (b==c)
return :equilateral
elsif (a==b) || (b==c) || (a==c)
return :isosceles
else
return :scalene
end
end
Hope it helps.
Reply

#10
def triangle(a, b, c)

sides = a, b, c # Assigns variable signs (array) to all arguments.
begin

raise TriangleError if sides.inject(:+) <= 0 # Raise an error if all sides added together are less than or equal to 0. (the triangle would be invalid).
raise TriangleError if sides.any?(&:negative?) #Raise an error if there are any negative sides.
sides.each {|side| (side < (sides.inject(:+) - side) ? nil : (raise TriangleError))} # For the final check, Raise an error if any single side is greater than the other two sides added together. It can be broken down like this if side is less than (remaining sides - side we're comparing) raise an error, else, nil.

return :equilateral if sides.uniq.length == 1
return :isosceles if sides.uniq.length == 2
return :scalene if sides.uniq.length == 3

resuce TriangleError
end
end
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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