Skip to content

Instantly share code, notes, and snippets.

@Andsbf
Created March 3, 2015 22:03
Show Gist options
  • Save Andsbf/4488e6a3f355fbd9eca6 to your computer and use it in GitHub Desktop.
Save Andsbf/4488e6a3f355fbd9eca6 to your computer and use it in GitHub Desktop.
# Find the maximum
def maximum(arr = 0)
#arr.max #Initial code (lighthouse code)
max_value = arr[0]
max_value = 0 if arr[0] == nil #Avoid empty array, makes answer zero
arr.each do |i|
max_value = i if (max_value <= i)
end
max_value
end
result = maximum([])
puts "Max of empty array #{result}"
# expect it to return 42 below
result = maximum([-1, -2, -3, -4])
puts "max of -1, -2, -3, -4 is: #{result}"
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