This file contains 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
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] |
This file contains 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(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 |
This file contains 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(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 |
NewerOlder