Created
January 3, 2013 07:55
-
-
Save kmandreza/4441660 to your computer and use it in GitHub Desktop.
Write a method count_between which takes three arguments as input: An Array of integers
An integer lower bound
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 is empty. Some examples: count_between([1,2,3], 0, 100) # => 3
count_bet…
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
| #1 first attempt | |
| def count_between(array, lower_bound, upper_bound) | |
| count = 0 | |
| array.each do |x| | |
| count += 1 if x >= lower_bound && x <= upper_bound | |
| end | |
| count | |
| end | |
| #2 second attempt | |
| def count_between(array, lower_bound, upper_bound) | |
| between = array.select {|x| x >= lower_bound && x <= upper_bound} | |
| between.count | |
| end | |
| #3 third attempt | |
| def count_between(array, lower_bound, upper_bound) | |
| array.select {|x| x >= lower_bound && x <= upper_bound}.count | |
| end | |
| #each attempt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment