Skip to content

Instantly share code, notes, and snippets.

@kenrett
kenrett / gist:5018587
Created February 23, 2013 05:27
mode
Exercise: Calculating the array mode
Write a method mode which takes an Array of numbers as its input and returns an Array of the most frequent values.
If there's only one most-frequent value, it returns a single-element Array.
For example,
mode([1,2,3,3]) # => [3]
mode([4.5, 0, 0]) # => [0]
mode([1.5, -1, 1, 1.5]) # => [1.5]
@kenrett
kenrett / gist:5016284
Created February 22, 2013 20:21
Count_between
def count_between(array, lower_bound, upper_bound)
if array.empty?
return 0
end
counter = 0
array.select { |num| counter += 1 if num >= lower_bound && num <= upper_bound }
end
counter
def count_between(array, lower_bound, upper_bound)
if array.empty?
return 0
end
array.each do |num|
if num >= lower_bound && num <= upper_bound
return num
else
return 0