-
Notifications
You must be signed in to change notification settings - Fork 36
FreqOfWords #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
FreqOfWords #45
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| freq = Hash.new(0) | ||
| file = File.open("speech.txt") | ||
| file.read.downcase.scan(/\b[a-z]{3,16}\b/){|word| freq[word] += 1} | ||
|
||
| freq = freq.sort_by{|a, b| b} | ||
| freq.reverse! | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .reverse made the counters go from 1 to 33. .reverse! allowed it to go from 33 to 1. |
||
| freq.each{|word, freq| puts freq.to_s + " - " + word} | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is something that I learned later on that you can initialize a hash with default values. Kind of cool. Well done.
Anytime you build up an empty collection, iteratively build it up then utilize it as input for something else, you should always consider
.reduceor a similar method as possibly a solution. in this case you're using.scanbut reduce would have worked as well as.each_with_object.http://batsov.com/articles/2013/12/04/using-rubys-each-with-object/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hash with default values 😮 😮