Ruby problem ⑦

This is the final episode.

problem

Given the three integers a b c, b or c has a difference of 1 from a And True if the difference between the values of b and c is 2 or more. Other than that, False Let's create a method that outputs.

Output example:

close_far(1, 2, 10) → True close_far(1, 2, 3) → False close_far(4, 1, 3) → True

Tips

Use the abs method to convert the return value to an integer.

abs You can get the absolute value by executing the "abs" method on the target number. In other words, if it is a positive number, it will remain as it is, but if it is a negative number, you can get the numerical value that is made into a positive number by taking the sign.

The actual usage is as follows.

num = 5.abs
#=> 5

num = (-5).abs
#=> 5


##### Model answer
def close_far(a,b,c)
  x = (a-b).abs
  y = (a-c).abs
  z = (b-c).abs

  if x == 1 && z >= 2
    puts "True"
  elsif y == 1 && z >= 2
    puts "True"
  else
    puts "False"
  end
end


There was no explanation this time, but it was understandable by looking at the answer. The point is to substitute the difference such as a-b. I couldn't think of it. I wanted to get to the correct answer ...

Recommended Posts

Ruby problem ⑦
ruby search problem
[Ruby] FizzBuzz problem
ruby API problem
[Ruby] FizzBuzz problem
[Ruby] Ruby API problem
ruby API problem
[Ruby] problem with if statement
Ruby deposit system, algorithm problem
Ruby learning 4
[N + 1 problem]
[Ruby] Array
Ruby basics
Ruby learning 5
Ruby basics
Calendar creation problem (fun Ruby practice problem)
Refactoring Ruby
Ruby learning 3
FizzBuzz problem
Ruby setting 2
Ruby learning 2
[Ruby] Block
Refactoring Ruby
ruby calculator
Ruby learning 6
[Ruby] Search problem using index method
Ruby settings 1
Refactoring Ruby
This problem is soberly difficult ... (Ruby)
Ruby basics
Ruby memo
Ruby learning 1
Ruby Review 1
[Ruby] Module
I tried a calendar problem in Ruby
[Problem] Weather on consecutive holidays (Ruby edition)
Introduction to Ruby 2
ruby Uppercase letters
Ruby Learning # 25 Comments
Ruby to_s method
[Ruby] postfix if
Ruby text conversion
Ruby basic terms
ruby exception handling
Ruby Learning # 13 Arrays
About Ruby symbols
Ruby Learning # 1 Introduction
Ruby Learning # 34 Modules
[ruby] drill output
I started Ruby
[Ruby] Iterative processing
Ruby vertical writing
About ruby ​​form
[ruby] drill output
Ruby Learning # 14 Hashes
[ruby] drill output
[Ruby] each nested
scan method problem
Ruby Hash notes
Class in Ruby
[Ruby] slice method