Skip to content

Instantly share code, notes, and snippets.

@efrenfuentes
Last active June 1, 2016 18:06
Show Gist options
  • Save efrenfuentes/2687afe410616324e7cffad326386bb4 to your computer and use it in GitHub Desktop.
Save efrenfuentes/2687afe410616324e7cffad326386bb4 to your computer and use it in GitHub Desktop.
Histogram
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