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?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework