Last active
December 27, 2015 03:19
-
-
Save roberthead/7258198 to your computer and use it in GitHub Desktop.
Some histogram-related methods for ruby Enumerable types.
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
module Enumerable | |
# Return a Hash whose keys are the unique elements and whose values are the number of appearences of that element. | |
# [2,1,2,3,4,1,2,4,9,1,3,16,8].histogram => {2=>3, 1=>3, 3=>2, 4=>2, 9=>1, 16=>1, 8=>1} | |
def histogram | |
inject(Hash.new(0)) { |hash, x| hash[x] += 1; hash } | |
end | |
# Return an enumerable with the same elements sorted from the most frequent to least frequent. | |
# See: http://codeidol.com/other/rubyckbk/Arrays/Sorting-an-Array-by-Frequency-of-Appearance/ | |
# [2,1,2,3,4,1,2,4,9,1,3,16,8].sort_by_frequency => [2, 2, 2, 1, 1, 1, 3, 3, 4, 4, 9, 16, 8] | |
def sort_by_frequency | |
h = histogram | |
sort_by { |x| [h[x] * -1, index(x)] } | |
end | |
# Return an enumerable with uniq elements sorted from the most frequent to least frequent. | |
# [2,1,2,3,4,1,2,4,9,1,3,16,8].uniq_by_frequency => [2, 1, 3, 4, 9, 16, 8] | |
# Note: This is much faster than calling sort_by_frequency.uniq | |
def uniq_by_frequency | |
h = histogram | |
uniq.sort_by { |x| [h[x] * -1, index(x)] } | |
end | |
# Return one from among the most frequent elements. | |
# [2,1,2,3,4,1,2,4,9,1,3,16,8].most_frequent_element => 2 | |
def most_frequent_element | |
uniq_by_frequency.first | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment