Skip to content

Instantly share code, notes, and snippets.

@imtiaz-emu
Created October 28, 2018 05:09
Show Gist options
  • Save imtiaz-emu/115345cf61787c872ce04f9258f1071b to your computer and use it in GitHub Desktop.
Save imtiaz-emu/115345cf61787c872ce04f9258f1071b to your computer and use it in GitHub Desktop.
Inside the file "http://tech2.bumbung.net/questions/article.txt" is an article that we would like to know the number of times each word has been shown.
str = gets.chomp # Take text input from console
# scan the text to remove punctuations and split
str = str.scan(/[\w']+/)
# create a Hash for storing the word count
words = Hash[str.uniq.map{ |i| [i, str.count(i)] }]
# print the number of times each word has been shown
for word, count in words
puts("#{word} = #{count}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment