Last active
June 1, 2016 18:06
-
-
Save efrenfuentes/2687afe410616324e7cffad326386bb4 to your computer and use it in GitHub Desktop.
Histogram
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 histogram_for(text) | |
words = text.split(' ') | |
frequency = Hash.new(0) | |
words.each { |word| frequency[word.downcase] += 1 } | |
frequency.sort_by {|_key, value| value}.to_h | |
end | |
# Read file | |
text = "word1 word2 word3" | |
histogram = histogram_for text | |
histogram.each do |word, frequency| | |
puts "#{word}: #{frequency}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment