Created
October 29, 2012 22:08
-
-
Save sgottlieb/3976865 to your computer and use it in GitHub Desktop.
Word count with Ruby
This file contains hidden or 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
def open_file(filename) | |
word_list = [] | |
aFile = File.open(filename, "r+") | |
aFile.each do |line| | |
list = line.split(" ") | |
list.each do |word| | |
word_list << word | |
end | |
end | |
aFile.close | |
return word_list | |
end | |
def make_dictionary(list_words) | |
word_dict = Hash.new(0) | |
list_words.each do |word| | |
word_dict[word] += 1 | |
end | |
return word_dict | |
end | |
def main(filename) | |
word_list = open_file(filename) | |
word_dict = make_dictionary(word_list) | |
return word_dict.sort | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment