Skip to content

Instantly share code, notes, and snippets.

@rbishop
Last active December 10, 2015 18:08
Show Gist options
  • Select an option

  • Save rbishop/4472013 to your computer and use it in GitHub Desktop.

Select an option

Save rbishop/4472013 to your computer and use it in GitHub Desktop.
Exercise: Count the numbers in an array between a given range Write a method count_between which takes three arguments as input: 1. An Array of integers 2. An integer lower bound 3. An integer upper bound count_between should return the number of integers in the Array between the two bounds, including the bounds It should return 0 if the Array i…
def count_between arr, lower, upper
return 0 if arr.length == 0 || lower > upper
return arr.length if lower == upper
range = (lower..upper).to_a
arr.select { |value| range.include?(value) }.length
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment