Skip to content

Instantly share code, notes, and snippets.

@fractalatcarf
Created April 3, 2017 09:54
Show Gist options
  • Save fractalatcarf/a0e196756445e070e414916094096ad5 to your computer and use it in GitHub Desktop.
Save fractalatcarf/a0e196756445e070e414916094096ad5 to your computer and use it in GitHub Desktop.
count words in a text file
article = open('article.txt').read
words = article.split(" ")
hash_words = {}
p words.length
words.each do |word|
if hash_words.key? (word)
hash_words[word] += 1
else
hash_words[word] = 1
end
end
sorted_hash = hash_words.sort_by do |word, frequency|
frequency
end
p sorted_hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment