Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TalkativeTree/5249867 to your computer and use it in GitHub Desktop.
Save TalkativeTree/5249867 to your computer and use it in GitHub Desktop.
putting two numbers into an array and have to count the numbers in between the upper and lower bound numbers.
# count_between is a method with three arguments:
# 1. An array of integers
# 2. An integer lower bound
# 3. An integer upper bound
#
# It returns the number of integers in the array between the lower and upper bounds,
# including (potentially) those bounds.
#
# If +array+ is empty the method should return 0
def count_between(array, lower_bound, upper_bound)
array << lower_bound << upper_bound
disc = array.sort.index(lower_bound.to_i)
drive = array.sort.index(upper_bound.to_i)
if lower_bound > upper_bound
return 0
elsif lower_bound == array.sort.first and upper_bound == array.sort.last
return array.length - 2
elsif (lower_bound != array.sort.first and upper_bound == array.sort.last) || (lower_bound == array.sort.first and upper_bound != array.sort.last)
return drive - disc - 1
end
end
puts count_between([], -100, 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment