Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save kmandreza/4441660 to your computer and use it in GitHub Desktop.

Select an option

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…
#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