Ruby Basic Exercises: Check three given integers x, y, z and return true if one of y or z is close while the other is far
Ruby Basic: Exercise-54 with Solution
Write a Ruby program to check three given integers x, y, z and return true if one of y or z is close (differing from a by at most 1), while the other is far, differing from both other values by 3 or more.
Ruby Code:
def check_num(x, y, z)
if (y-z).abs < 3
return false
end
return (x-y).abs<=1 && (x-z).abs>=3 || (x-z).abs<=1 && (x-y).abs>=3
end
print check_num(2, 3, 11),"\n"
print check_num(2, 3, 4),"\n"
print check_num(5, 2, 4)
Output:
true false false
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to check three given integers and compute their sum. However, if one of the values is 17 then it does not count towards the sum and values to its right do not count.
Next: Write a Ruby program to check three given integers and return true if the three values are evenly spaced, so the difference between small and medium is the same as the difference between medium and large.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/ruby-exercises/basic/ruby-basic-exercise-54.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics