Skip to content

Instantly share code, notes, and snippets.

@kenrett
Created February 22, 2013 20:21
Show Gist options
  • Save kenrett/5016284 to your computer and use it in GitHub Desktop.
Save kenrett/5016284 to your computer and use it in GitHub Desktop.
Count_between
def count_between(array, lower_bound, upper_bound)
if array.empty?
return 0
end
counter = 0
array.select { |num| counter += 1 if num >= lower_bound && num <= upper_bound }
end
counter
puts count_between([1,2,3], 0, 100) # => 3
puts count_between([-10, 1, 2], 0, 100) # => 2
puts count_between([10, 20, 30], 10, 30) # => 3
puts count_between([], -100, 100) # => 0
puts count_between([0], 0, 0) # => 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment