Last active
April 27, 2016 02:02
-
-
Save charmainetham/880d18318b6aaa125624495048db0c8d to your computer and use it in GitHub Desktop.
Maximum Value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Find the maximum | |
def maximum(arr) | |
# Setting the value as max value at first | |
max_value = arr.first | |
# For each value in the array the highest value would be x if x is higher than the first value | |
arr.each{|x| max_value = x if x > max_value} | |
max_value | |
end | |
# expect it to return 42 below | |
result = maximum([2, 42, 22, 02]) | |
puts "max of 2, 42, 22, 02 is: #{result}" | |
# expect it to return nil when empty array is passed in | |
result = maximum([]) | |
puts "max on empty set is: #{result.inspect}" | |
result = maximum([-23, 0, -3]) | |
puts "max of -23, 0, -3 is: #{result}" | |
result = maximum([6]) | |
puts "max of just 6 is: #{result}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment