find_max code refactor

This commit is contained in:
Sahil Afrid Farookhi 2021-04-21 15:24:43 +05:30
parent 040b917b3d
commit 0c1892d4ef

View file

@ -1,6 +1,5 @@
# A ruby program to find max from a set of elements # A ruby program to find max from a set of elements
module FindMax
# This find_max method will return the max element out of the array # This find_max method will return the max element out of the array
def self.find_max(*array) def self.find_max(*array)
max = array[0] max = array[0]
@ -9,58 +8,57 @@ module FindMax
max = a max = a
end end
end end
puts "The Max of the following elements #{array} is #{max}." return "The Max of the following elements #{array} is #{max}."
rescue rescue
puts "Error: Please provide number only!" return "Error: Please provide number only!"
end end
# The max method will return the maximum element from the set of input elements provided # Max method will return the maximum element from the set of input elements provided
def self.predefined_max(*array) def self.predefined_max(*array)
puts "The Max of the following elements #{array} is #{array.max}." return "The Max of the following elements #{array} is #{array.max}."
rescue rescue
puts "Error: Please provide number only!" return "Error: Please provide number only!"
end end
# The sort method will sort the elements in ascending order. And the last method will return the end element out of the array # Sort method will sort the elements in ascending order. Last method will return the end element out of the array
def self.predefined_sort_last_max(*array) def self.predefined_sort_last_max(*array)
puts "The Max of the following elements #{array} is #{array.sort.last}." return "The Max of the following elements #{array} is #{array.sort.last}."
rescue rescue
puts "Error: Please provide number only!" return "Error: Please provide number only!"
end
end end
# Using find_max # Using find_max
# Valid inputs # Valid inputs
FindMax.find_max(11, 29, 33) puts find_max(11, 29, 33)
# The Max of the following elements [11, 29, 33] is 33. # The Max of the following elements [11, 29, 33] is 33.
FindMax.find_max(-221, -852, -1100, -10) puts find_max(-221, -852, -1100, -10)
# The Max of the following elements [-221, -852, -1100, -10] is -10. # The Max of the following elements [-221, -852, -1100, -10] is -10.
# Invalid inputs # Invalid inputs
FindMax.find_max(5, "95", 2) puts find_max(5, "95", 2)
# Error: Please provide number only! # Error: Please provide number only!
# Using predefined_max # Using predefined_max
# Valid inputs # Valid inputs
FindMax.predefined_max(51, 82, 39) puts predefined_max(51, 82, 39)
# The Max of the following elements [51, 82, 39] is 82. # The Max of the following elements [51, 82, 39] is 82.
FindMax.predefined_max(-11, -51, -10, -10) puts predefined_max(-11, -51, -10, -10)
# The Max of the following elements [-11, -51, -10, -10] is -10. # The Max of the following elements [-11, -51, -10, -10] is -10.
# Invalid inputs # Invalid inputs
FindMax.predefined_max("x", 5, 95, 2) puts predefined_max("x", 5, 95, 2)
# Error: Please provide number only! # Error: Please provide number only!
# Using predefined_sort_last_max # Using predefined_sort_last_max
# Valid inputs # Valid inputs
FindMax.predefined_sort_last_max(1, 2, 3) puts predefined_sort_last_max(1, 2, 3)
# The Max of the following elements [1, 2, 3] is 3. # The Max of the following elements [1, 2, 3] is 3.
FindMax.predefined_sort_last_max(-21, -52, -100, -1) puts predefined_sort_last_max(-21, -52, -100, -1)
# The Max of the following elements [-21, -52, -100, -1] is -1. # The Max of the following elements [-21, -52, -100, -1] is -1.
# Invalid inputs # Invalid inputs
FindMax.predefined_sort_last_max(5, 95, 2, "a") puts predefined_sort_last_max(5, 95, 2, "a")
# Error: Please provide number only! # Error: Please provide number only!