Created
February 22, 2013 20:21
-
-
Save kenrett/5016284 to your computer and use it in GitHub Desktop.
Count_between
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
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