Created
October 28, 2018 05:09
-
-
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.
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
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