Skip to content

Instantly share code, notes, and snippets.

@kmandreza
Created January 3, 2013 08:12
Show Gist options
  • Select an option

  • Save kmandreza/4441747 to your computer and use it in GitHub Desktop.

Select an option

Save kmandreza/4441747 to your computer and use it in GitHub Desktop.
Write a method smallest_integer which takes as its input an Array of integers and returns the smallest integer in the Array. For example: smallest_integer([1, 2, 3]) # => 1 smallest_integer([0, -10, 10]) # => -10 smallest_integer([-10, -20, -30]) # => -30 If the input Array is empty smallest_integer should return nil.
# smallest_integer is a method that takes an array of integers as its input
# and returns the smallest integer in the array
#
# +array+ is an array of integers
# smallest_integer(array) should return the smallest integer in +array+
#
# If +array+ is empty the method should return nil
def smallest_integer(array)
min = nil
array.each do |x|
if min.nil? || min > x
min = x
end
end
min
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment