mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-29 20:34:27 +01:00
Merge pull request #154 from msaf9/master
abs min feature implementation
This commit is contained in:
commit
269631278e
2 changed files with 36 additions and 0 deletions
|
@ -79,6 +79,8 @@
|
|||
* [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/fibonacci.rb)
|
||||
|
||||
## Maths
|
||||
* [Abs Max](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs_max.rb)
|
||||
* [Abs Min](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs_min.rb)
|
||||
* [Abs](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs.rb)
|
||||
* [Abs Max](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs_max.rb)
|
||||
* [Abs Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs_test.rb)
|
||||
|
|
34
maths/abs_min.rb
Normal file
34
maths/abs_min.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
# A ruby program to find absolute minimum
|
||||
# Mathematical representation of abs min = ((a + b - absoulte(a - b)) / 2)
|
||||
|
||||
def abs_min(x, y)
|
||||
num = x - y
|
||||
min_value = ((x + y - num.abs()) / 2)
|
||||
"The Abs Min of #{x} and #{y} is #{min_value}."
|
||||
rescue
|
||||
"Error: Provide number only!"
|
||||
end
|
||||
|
||||
#
|
||||
# Valid inputs
|
||||
#
|
||||
puts abs_min(10, 20)
|
||||
# The Abs Min of 10 and 20 is 10.
|
||||
|
||||
puts abs_min(-10, -1)
|
||||
# The Abs Min of -10 and -1 is -10.
|
||||
|
||||
puts abs_min(9, -121)
|
||||
# The Abs Min of 9 and -121 is -121.
|
||||
|
||||
#
|
||||
# Invalid inputs
|
||||
#
|
||||
puts abs_min(2, "-1")
|
||||
# Error: Provide number only!
|
||||
|
||||
puts abs_min("3", "5")
|
||||
# Error: Provide number only!
|
||||
|
||||
puts abs_min("a", "5")
|
||||
# Error: Provide number only!
|
Loading…
Add table
Reference in a new issue