Skip to content

Instantly share code, notes, and snippets.

@McTano
Forked from davidvandusen/max.rb
Last active May 24, 2016 20:22
Show Gist options
  • Save McTano/2d39ddd37b83d19f27ac08d23c12dae3 to your computer and use it in GitHub Desktop.
Save McTano/2d39ddd37b83d19f27ac08d23c12dae3 to your computer and use it in GitHub Desktop.
require 'pry'
# Find the maximum
def maximum(arr)
# arr.max
big = arr[0]
arr.each do |x|
big = x if big < x
end
big
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}"
#expect 0
result = maximum([-23, 0, -3])
puts "max of -23, 0, -3 is: #{result}"
#expect 6
result = maximum([6])
puts "max of just 6 is: #{result}"
## expected output
# max of 2, 42, 22, 02 is: 42
# max on empty set is: nil
# max of -23, 0, -3 is: 0
# max of just 6 is: 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment