Last active
December 10, 2015 18:08
-
-
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…
This file contains hidden or 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 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