Simple ruby syntax if statement. Is this possible? - ruby

I'm really new to programming and would like to know if this is possible:
def lock(a,b,c,d)
if (a == 3 || a == 5 || a == 7 && b == 2 && c == 5 || c == 6 && d == 8 || d == 9 || d == 0)
"unlocked"
else
"locked"
end
If it isn't possible, why?
So when I pass in: lock(3,1,1,1) it should be locked.
Should I do this differently? Is there a better way?

I would use include? to make it more readable:
if ([3,5,7].include?a and b == 2 and [5,6].include?c and [8,9,0].include?d)

You should have to add bracket properly as per your logic. Like this,
((a == 3 || a == 5 || a == 7) && (b == 2) && (c == 5 || c == 6) && (d == 8 || d == 9 || d == 0))

Related

Give an if statement a variable in Ruby

I have this if statement:
if value_n_in_f > 0 && value_m_in_f != 1
puts "I: f(x)=#{value_m_in_f}x+#{value_n_in_f}"
elsif value_n_in_f == 0 && value_m_in_f != 1
puts "I: f(x)=#{value_m_in_f}x"
elsif value_n_in_f < 0 && value_m_in_f != 1
puts "I: f(x)=#{value_m_in_f}x#{value_n_in_f}"
elsif value_n_in_f > 0 && value_m_in_f == 1
puts "I: f(x)=x+#{value_n_in_f}"
elsif value_n_in_f == 0 && value_m_in_f == 1
puts "I: f(x)=x"
elsif value_n_in_f < 0 && value_m_in_f == 1
puts "I: f(x)=x#{value_n_in_f}"
end`
I have to use this statement really often in other statements and it makes my code unnecessarily long. Obviously if_for_f=if ....... end won't work. Is there any other way?
Here's an example what I want it to look like:
puts "Now we insert #{value_of_x} in #{if_for_f}"
Is there any way I can do something like this? Note that I am absolutly new to this.
Thanks in advance,
Kaiton
can't I just def a function with its function to run this case
statement and call it then when I need it it?
Of course:
def do_stuff(m, n)
if m == 1
if n > 0 then "I: f(x)=x+#{n}"
elsif n == 0 then "I: f(x)=x"
elsif n < 0 then "I: f(x)=x#{n}"
end
else
if n > 0 then "I: f(x)=#{m}x+#{n}"
elsif n == 0 then "I: f(x)=#{m}x"
elsif n < 0 then "I: f(x)=#{m}x#{n}"
end
end
end
puts do_stuff(1, 1) #I: f(x)=x+1
Or, if compactness is the goal, we can get to this:
def do_stuff(m, n)
if m == 1
n == 0 ? "I: f(x)=x" : "I: f(x)=x#{sprintf("%+d", n)}"
else
n == 0 ? "I: f(x)=#{m}x" : "I: f(x)=#{m}x#{sprintf("%+d", n)}"
end
end
...and then a one liner:
def do_stuff(m, n)
(m == 1) ? (n == 0 ? "I: f(x)=x" : "I: f(x)=x#{sprintf("%+d", n)}") : (n == 0 ? "I: f(x)=#{m}x" : "I: f(x)=#{m}x#{sprintf("%+d", n)}")
end
end
But your methodology has some problems with zeros and -1:
def do_stuff(value_m_in_f, value_n_in_f)
if value_n_in_f > 0 && value_m_in_f != 1
puts "I: f(x)=#{value_m_in_f}x+#{value_n_in_f}"
elsif value_n_in_f == 0 && value_m_in_f != 1
puts "I: f(x)=#{value_m_in_f}x"
elsif value_n_in_f < 0 && value_m_in_f != 1
puts "I: f(x)=#{value_m_in_f}x#{value_n_in_f}"
elsif value_n_in_f > 0 && value_m_in_f == 1
puts "I: f(x)=x+#{value_n_in_f}"
elsif value_n_in_f == 0 && value_m_in_f == 1
puts "I: f(x)=x"
elsif value_n_in_f < 0 && value_m_in_f == 1
puts "I: f(x)=x#{value_n_in_f}"
end
end
do_stuff(1, 0)
do_stuff(1,-1)
do_stuff(1, 1)
do_stuff(0,-1)
do_stuff(0, 0)
do_stuff(-1, 1)
--output:--
I: f(x)=x
I: f(x)=x-1
I: f(x)=x+1
I: f(x)=0x-1 #<---HERE
I: f(x)=0x #<---HERE
I: f(x)=-1x+1 #<---HERE
So let's fix that:
def get_line_equ(m, b)
constant = (b == 0) ? "" : sprintf("%+d", b) # 2 => "+2"
case m
when 0
xterm = ""
constant = b
when 1
xterm = "x"
when -1
xterm = "-x"
else
xterm = "#{m}x"
end
"I: f(x)=#{xterm}#{constant}"
end
puts get_line_equ(0, 0)
puts get_line_equ(0, -1)
puts get_line_equ(0, 1)
puts get_line_equ(1, 0)
puts get_line_equ(1,-1)
puts get_line_equ(1, 1)
puts get_line_equ(-1, 0)
puts get_line_equ(-1, -1)
puts get_line_equ(-1, 1)
puts get_line_equ(2, 0)
puts get_line_equ(2, -1)
puts get_line_equ(2, 1)
--output:--
I: f(x)=0
I: f(x)=-1
I: f(x)=1
I: f(x)=x
I: f(x)=x-1
I: f(x)=x+1
I: f(x)=-x
I: f(x)=-x-1
I: f(x)=-x+1
I: f(x)=2x
I: f(x)=2x-1
I: f(x)=2x+1
Better?
spoiler:
The final def is not as efficient as it could be: the first line should be removed and copied to each of the when branches--except for the first.
In response to comment:
def my_sprintf(str, *numbers)
str.gsub(/% .*? [df]/x) do |match| #Looks for %...d or %...f sequences
puts match
end
end
my_sprintf("The answer is: %+d or %+d", -2, 3)
--output:--
%+d
%+d
Next:
def my_sprintf(str, *numbers)
str.gsub(/% .*? [df]/x) do |format_sequ|
number_as_str = numbers.shift.to_s
p number_as_str
if format_sequ[1] == "+" and number_as_str[0] != "-"
"+#{number_as_str}"
else
number_as_str
end
end
end
puts my_sprintf("The answer is: %+d or %+d.", -2, 3)
--output:--
"-2"
"3"
The answer is: -2 or +3.
Here's a more compact way to write your case statement. Recall that a <=> b => -1 if a < b, a <=> b => 0 if a == b and a <=> b => -1 if a > b.
"I: f(x)=" +
case [value_n_in_f <=> 0, value_m_in_f == 1]
when [ 1,false] then "#{value_m_in_f}x+#{value_n_in_f}"
when [ 0,false] then "#{value_m_in_f}x"
when [-1,false] then "#{value_m_in_f}x#{value_n_in_f}"
when [ 1,true] then "x+#{value_n_in_f}"
when [ 0,true] then "x"
when [-1,true] then "x#{value_n_in_f}"
end
If you wish to demonstrate how the string is built, you could do something like this (with value_n_in_f and value_m_in_f renamed intercept and slope, respectively):
"I: f(x)=" +
case
when slope.zero?
intercept.zero? ? "0" : "#{intercept}"
else
case slope.to_f
when 1.0 then ""
when -1.0 then "-"
else "#{slope}"
end + "x" +
case intercept <=> 0
when 0 then ""
when -1 then "#{intercept}"
else "+#{intercept}"
end
end
Note that this permits slope < 0, which is not part of the specification. I tested this for various combinations of intercept and slope:
intercept slope string
-2.1 4 I: f(x)=4x-2.1
-2.1 -2.2 I: f(x)=-2.2x-2.1
-2.1 0 I: f(x)=-2.1
-2.1 0.0 I: f(x)=-2.1
-2.1 -1 I: f(x)=-x-2.1
-2.1 1.0 I: f(x)=x-2.1
0 4 I: f(x)=4x
0 -2.2 I: f(x)=-2.2x
0 0 I: f(x)=0
0 0.0 I: f(x)=0
0 -1 I: f(x)=-x
0 1.0 I: f(x)=x
0.0 4 I: f(x)=4x
0.0 -2.2 I: f(x)=-2.2x
0.0 0 I: f(x)=0
0.0 0.0 I: f(x)=0
0.0 -1 I: f(x)=-x
0.0 1.0 I: f(x)=x
3 4 I: f(x)=4x+3
3 -2.2 I: f(x)=-2.2x+3
3 0 I: f(x)=3
3 0.0 I: f(x)=3
3 -1 I: f(x)=-x+3
3 1.0 I: f(x)=x+3
A couple of things here: you can put "puts" before the whole if/elsif block and avoid having puts on every line, like so:
puts case
when (value_n_in_f > 0 && value_m_in_f != 1) then "I: f(x)=#{value_m_in_f}x+#{value_n_in_f}"
when (value_n_in_f == 0 && value_m_in_f != 1) then "I: f(x)=#{value_m_in_f}x"
end
Second, a case statement would be much more readable, like so:
def big_compare(value_n_in_f, value_m_in_f)
msg = case
when (value_n_in_f > 0 && value_m_in_f != 1) then "I: f(x)=#{value_m_in_f}x+#{value_n_in_f}"
when (value_n_in_f == 0 && value_m_in_f != 1) then "I: f(x)=#{value_m_in_f}x"
when (value_n_in_f < 0 && value_m_in_f != 1) then "I: f(x)=#{value_m_in_f}x#{value_n_in_f}"
when (value_n_in_f > 0 && value_m_in_f == 1) then "I: f(x)=x+#{value_n_in_f}"
when (value_n_in_f == 0 && value_m_in_f == 1) then "I: f(x)=x"
when (value_n_in_f < 0 && value_m_in_f == 1) then "I: f(x)=x#{value_n_in_f}"
end
end
puts big_compare(0, 0)

Ruby OOP tic-tac-toe hash / conditional / itiration

Trying to create a classic tic tac toe OOP in Ruby but am having trouble with my game_results() method.
I realize this is not really complete and needs some more functionality but for now im just trying to fill my board with the inputted objects from the user and outputting the filled board and winner.
When I can see on my board that we have a winner I call for the game_results() method and it gives me the correct winner but whenever a tie game occurs, I receive a error.
Anybody got any ideas or solutions to what I'm doing wrong with the operators? Btw I know all this is pretty messy, but im a beginner.
#require "pry"
class Game
attr_reader :turn
def initialize
#turn = 1
#board = { a1: " ", a2: " ", a3: " ", b1: " ", b2: " ", b3: " ", c1: " ", c2: " ", c3: " " }
end
# def start_game
# x = Game.new
# x.player_symbol
# x.player_turn
# x.check_game
# end
def intro
puts "ULTIMATE TIC TAC TOE .. in ruby!\n"
display_board
end
def player_symbol
puts "Player 1, choose your marker. X or O?\n"
i = gets.chomp.upcase
if i == "X"
#p1 = "X"
#p2 = "O"
elsif i == "O"
#p1 = "O"
#p2 = "X"
else
puts "That is not a valid player!"
end
end
def player_turn
if #turn == 1
puts "Player 1 turn."
#turn = 2
player_move(#p1)
else
puts "Player 2 turn."
#turn = 1
player_move(#p2)
end
end
def display_board
# Displays board grid using hash values.
puts " 1 2 3 "
puts "A #{#board[:a1]} | #{#board[:a2]} | #{#board[:a3]} "
puts " ---+---+---"
puts "B #{#board[:b1]} | #{#board[:b2]} | #{#board[:b3]} "
puts " ---+---+---"
puts "C #{#board[:c1]} | #{#board[:c2]} | #{#board[:c3]} "
end
def player_move(n)
# Player picks square to claim.
# Player symbol populates hash.
puts "pick move"
x = gets.chomp.downcase.to_sym
#board[x] = "#{n}" # Directs to player X/O
display_board
end
def game_results
# Determines winner, loser, or tie game
if ((#board[:a1] == #p1) && (#board[:a2] == #p1) && (#board[:a3] == #p1)) ||
((#board[:b1] == #p1) && (#board[:b2] == #p1) && (#board[:b3] == #p1)) ||
((#board[:c1] == #p1) && (#board[:c2] == #p1) && (#board[:c3] == #p1)) ||
((#board[:a1] == #p1) && (#board[:b1] == #p1) && (#board[:c1] == #p1)) ||
((#board[:a2] == #p1) && (#board[:b2] == #p1) && (#board[:c2] == #p1)) ||
((#board[:a3] == #p1) && (#board[:b3] == #p1) && (#board[:c3] == #p1)) ||
((#board[:a1] == #p1) && (#board[:b2] == #p1) && (#board[:c3] == #p1)) ||
((#board[:a3] == #p1) && (#board[:b2] == #p1) && (#board[:c1] == #p1))
puts "Player #{#p1} is the winner!"
elsif ((#board[:a1] == #p2) && (#board[:a2] == #p2) && (#board[:a3] == #p2)) ||
((#board[:b1] == #p2) && (#board[:b2] == #p2) && (#board[:b3] == #p2)) ||
((#board[:c1] == #p2) && (#board[:c2] == #p2) && (#board[:c3] == #p2)) ||
((#board[:a1] == #p2) && (#board[:b1] == #p2) && (#board[:b1] == #p2)) ||
((#board[:a2] == #p2) && (#board[:b2] == #p2) && (#board[:c2] == #p2)) ||
((#board[:a3] == #p2) && (#board[:b3] == #p2) && (#board[:c3] == #p2)) ||
((#board[:a1] == #p2) && (#board[:b2] == #p2) && (#board[:c3] == #p2)) ||
((#board[:a3] == #p2) && (#board[:b2] == #p2) && (#board[:c1] == #p2))
puts "Player #{#p2} is the winner!"
elsif ((#board[:a1] == ("X" || "O")) &&
(#board[:a2] == ("X" || "O")) &&
(#board[:a3] == ("X" || "O")) &&
(#board[:b1] == ("X" || "O")) &&
(#board[:b2] == ("X" || "O")) &&
(#board[:b3] == ("X" || "O")) &&
(#board[:c1] == ("X" || "O")) &&
(#board[:c2] == ("X" || "O")) &&
(#board[:c3] == ("X" || "O")))
# This should represent that empty elements in the hash are now filled not making a winning pair
puts "Tie Game"
else
player_turn
end
end
end

Initialize Array with Values (Ruby)

I was wondering if there was a way to initialize this array in one line:
mulof = []
(1..1000).each {|i| mulof << i if (i % 3 == 0 || i % 5 == 0)}
Yes there is using Enumerable#select :
mulof = (1..1000).select { |i| i % 3 == 0 || i % 5 == 0 }

Ruby Koan 151 raising exceptions

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.
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.
Negative length doesn't make sense.
Any two sides of a triangle should add up to more than the third side.
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.
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
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 :)
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
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)
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
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.
After try to understand what I must to do with koan 151, I got it with the first posts, and get lot fun to check everyone solution :) ... here is the mine:
def triangle(a, b, c)
array = [a, b, c].sort
raise TriangleError if array.min <= 0 || array[0]+array[1] <= array[2]
array.uniq!
array.length == 1 ? :equilateral: array.length == 2 ? :isosceles : :scalene
end
Koan is a very interesting way to learn Ruby
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
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
I don't think I see this one here, yet.
I believe all the illegal triangle conditions imply that the longest side can't be more than half the total. i.e:
def triangle(a, b, c)
fail TriangleError, "Illegal triangle: [#{a}, #{b}, #{c}]" if
[a, b, c].max >= (a + b + c) / 2.0
return :equilateral if a == b and b == c
return :isosceles if a == b or b == c or a == c
return :scalene
end
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
I ended up with this code:
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.
You could also try to instance the exception with:
raise TriangleError.new("All sides must be greater than 0") if a * b * c <= 0
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
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
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
Here is my version... :-)
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
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
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
#(1)Any zero or -ve values
if [a,b,c].any? { |side_length| side_length <= 0 }
raise TriangleError
end
#(2)Any side of a triangle must be less than the sum of the other two sides
# a < b+c, b < a+c and c < a+b a valid triangle
# a >= b+c, b >= a+c and c >= a+b an invalid triangle
total_of_side_lengths = [a,b,c].inject {|total,x| total += x}
if [a,b,c].any? { |side_length| side_length >= (total_of_side_lengths - side_length)}
raise TriangleError
end
Not that this question needed another answer; however, I think this is the simplest and most readable solution. Thanks to all those before me.
def triangle(a, b, c)
a, b, c = [a, b, c].sort
raise TriangleError, "all sides must > 0" unless [a, b, c].min > 0
raise TriangleError, "2 smaller sides together must the > 3rd side" unless a + b > c
return :equilateral if a == b && a == 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
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
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
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
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
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
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
Here's my solution... honestly I can't think of a more concise and readable one!
def triangle(a, b, c)
raise TriangleError unless a > 0 && b > 0 && c > 0
raise TriangleError if a == b && a + b <= c
raise TriangleError if a == c && a + c <= b
return :equilateral if a == b && b == c
return :isosceles if a == b || b == c || c == a
:scalene
end
Rules:
size must be > 0
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)

Unreproducible string comparison, forces elsif failure

In answering this code golf question, I ran across a problem in my answer.
I've been testing this and I cannot even get these two comparisons to work in the code, despite the fact that IRB has the right behavior. I really need some help here.
Here's the code, below that will be an explanation of the problem.
def solve_expression(expr)
chars = expr.split '' # characters of the expression
parts = [] # resulting parts
s,n = '','' # current characters
while(n = chars.shift)
if (s + n).match(/^(-?)[.\d]+$/) || (!chars[0].nil? && chars[0] != ' ' && n == '-') # only concatenate when it is part of a valid number
s += n
elsif (chars[0] == '(' && n[0] == '-') || n == '(' # begin a sub-expression
p n # to see what it breaks on, ( or -
negate = n[0] == '-'
open = 1
subExpr = ''
while(n = chars.shift)
open += 1 if n == '('
open -= 1 if n == ')'
# if the number of open parenthesis equals 0, we've run to the end of the
# expression. Make a new expression with the new string, and add it to the
# stack.
subExpr += n unless n == ')' && open == 0
break if open == 0
end
parts.push(negate ? -solve_expression(subExpr) : solve_expression(subExpr))
s = ''
elsif n.match(/[+\-\/*]/)
parts.push(n) and s = ''
else
parts.push(s) if !s.empty?
s = ''
end
end
parts.push(s) unless s.empty? # expression exits 1 character too soon.
# now for some solutions!
i = 1
a = parts[0].to_f # left-most value is will become the result
while i < parts.count
b,c = parts[i..i+1]
c = c.to_f
case b
when '+': a = a + c
when '-': a = a - c
when '*': a = a * c
when '/': a = a / c
end
i += 2
end
a
end
The problem occurs in the assignment of negate.
I need negate to be true when the character just before a expression is a dash, but the condition isn't even working. Both n == '-' and n[0] == '-', the form of quotation doesn't matter, wind up FALSE every time. Yet, I've been using this exact comparison and n == '(' works correctly every time!
What is going on? Why doesn't n == '-' work, when n == '(' does? This is encoded in UTF-8 w/o BOM, UNIX linebreaks.
What's wrong with my code?
You have:
if (s + n).match(/^(-?)[.\d]+$/) || (!chars[0].nil? && chars[0] != ' ' && n == '-')
s += n
elsif (chars[0] == '(' && n[0] == '-') || n == '('
As n is always a one-character string, if (chars[0] == '(' && n[0] == '-')) is true, then the previous condition, (!chars[0].nil? && chars[0] != ' ' && n == '-'), will also be true. Your code will never enter the second part of the if if n[0]=='-'.
If your p n line is outputting a dash, be sure it is exactly the same character you are looking for, not some character that looks like a dash. Unicode has many kinds of dashes, maybe you have a weird unicode dash character in your code or on your input.

Resources