mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-11-16 19:50:00 +01:00
find min feature implementation
This commit is contained in:
parent
9b6a1252e2
commit
1ec08e7207
2 changed files with 65 additions and 0 deletions
|
@ -55,6 +55,7 @@
|
|||
* [Decimal To Binary](https://github.com/TheAlgorithms/Ruby/blob/master/maths/decimal_to_binary.rb)
|
||||
* [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/maths/fibonacci.rb)
|
||||
* [Find Max](https://github.com/TheAlgorithms/Ruby/blob/master/maths/find_max.rb)
|
||||
* [Find Min](https://github.com/TheAlgorithms/Ruby/blob/master/maths/find_min.rb)
|
||||
* [Number Of Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/number_of_digits.rb)
|
||||
* [Power Of Two](https://github.com/TheAlgorithms/Ruby/blob/master/maths/power_of_two.rb)
|
||||
* [Square Root](https://github.com/TheAlgorithms/Ruby/blob/master/maths/square_root.rb)
|
||||
|
|
64
maths/find_min.rb
Normal file
64
maths/find_min.rb
Normal file
|
@ -0,0 +1,64 @@
|
|||
# A ruby program to find min from a set of elements
|
||||
|
||||
# This find_min method will return the min element out of the array
|
||||
def find_min(*array)
|
||||
min = array[0]
|
||||
array.each do |a|
|
||||
if a <= min
|
||||
min = a
|
||||
end
|
||||
end
|
||||
"The Min of the following elements #{array} is #{min}."
|
||||
rescue
|
||||
"Error: Please provide number only!"
|
||||
end
|
||||
|
||||
# Min method will return the minimum element from the set of input elements provided
|
||||
def predefined_min(*array)
|
||||
"The Min of the following elements #{array} is #{array.min}."
|
||||
rescue
|
||||
"Error: Please provide number only!"
|
||||
end
|
||||
|
||||
# Sort method will sort the elements in ascending order. First method will return the beginning element out of the array
|
||||
def predefined_sort_first_min(*array)
|
||||
"The Min of the following elements #{array} is #{array.sort.first}."
|
||||
rescue
|
||||
"Error: Please provide number only!"
|
||||
end
|
||||
|
||||
# Using find_min
|
||||
# Valid inputs
|
||||
puts find_min(11, 29, 33)
|
||||
# The Min of the following elements [11, 29, 33] is 33.
|
||||
|
||||
puts find_min(-221, -852, -1100, -10)
|
||||
# The Min of the following elements [-221, -852, -1100, -10] is -10.
|
||||
|
||||
# Invalid inputs
|
||||
puts find_min(5, "95", 2)
|
||||
# Error: Please provide number only!
|
||||
|
||||
# Using predefined_min
|
||||
# Valid inputs
|
||||
puts predefined_min(51, 82, 39)
|
||||
# The Min of the following elements [51, 82, 39] is 82.
|
||||
|
||||
puts predefined_min(-11, -51, -10, -10)
|
||||
# The Min of the following elements [-11, -51, -10, -10] is -10.
|
||||
|
||||
# Invalid inputs
|
||||
puts predefined_min("x", 5, 95, 2)
|
||||
# Error: Please provide number only!
|
||||
|
||||
# Using predefined_sort_first_min
|
||||
# Valid inputs
|
||||
puts predefined_sort_first_min(1, 2, 3)
|
||||
# The Min of the following elements [1, 2, 3] is 3.
|
||||
|
||||
puts predefined_sort_first_min(-21, -52, -100, -1)
|
||||
# The Min of the following elements [-21, -52, -100, -1] is -1.
|
||||
|
||||
# Invalid inputs
|
||||
puts predefined_sort_first_min(5, 95, 2, "a")
|
||||
# Error: Please provide number only!
|
Loading…
Reference in a new issue