Skip to content

Instantly share code, notes, and snippets.

@arthur-littm
Created October 6, 2017 17:25
Show Gist options
  • Save arthur-littm/694c36573868690f5fb8f570f4f7e493 to your computer and use it in GitHub Desktop.
Save arthur-littm/694c36573868690f5fb8f570f4f7e493 to your computer and use it in GitHub Desktop.
text = open('article.txt').read
#create hash
histogram = {}
words = text.split
special_chars = (" ".."@").to_a
special_chars << ":"
clean_words = []
words.map do |word|
split_word = word.split("")
split_word.each do |letter|
if special_chars.include?(letter)
split_word.delete(letter)
clean_words << split_word.join
else
clean_words << split_word.join
end
end
end
clean_words.each do |word|
word_count = clean_words.count(word)
histogram[word] = word_count
end
sorted_histogram = histogram.sort_by do |key, value|
-value
end
p sorted_histogram
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment