-
Notifications
You must be signed in to change notification settings - Fork 36
Andrew Petit Solution #44
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?
Changes from all commits
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,31 @@ | ||
|
|
||
| def readFile(file) | ||
| return File.read(file) | ||
| end | ||
|
|
||
|
|
||
| def getWords(text) | ||
| text.gsub!(/[^A-Za-z ]/,'') | ||
|
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.
|
||
| data = Hash.new | ||
|
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. Anytime you initialize a new object and then build it up is a good time to ask could I use In this case, there's an even better option |
||
| temparray = text.to_s.split(" ").map(&:to_s) | ||
| for word in temparray | ||
|
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. You won't see a lot of "for loops" used in ruby. There's usually a better option available if you sniff around in... https://ruby-doc.org/core-2.4.1/Enumerable.html |
||
| if(data.has_key?(word)) | ||
| data[word] += 1 | ||
| else | ||
| data[word] = 1 | ||
| end | ||
| end | ||
| return data | ||
| end | ||
|
|
||
| def printWords(data) | ||
| data = data.sort_by { |key, value| value }.reverse | ||
| data.each do |key, value| | ||
| puts key.to_s + ' ' + value.to_s | ||
| end | ||
| end | ||
|
|
||
|
|
||
| text = readFile("/Users/apetit/CountingWords/speech.txt") | ||
| data = getWords(text) | ||
| printWords(data) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| require 'rspec' | ||
| require_relative '../lib/readfile.rb' | ||
|
|
||
| describe 'Number of words' do | ||
| it 'get words return the proper number' do | ||
|
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. 2 spaces. https://github.com/bbatsov/ruby-style-guide Check out this great site on rspec best practices. |
||
| expect(getWords("one two three four").count).to eq 4 | ||
| end | ||
| end | ||
|
|
||
| describe 'Accounts for duplicates' do | ||
| it 'same word only gets counted once' do | ||
| expect(getWords("one two three one")["one"]).to eq 2 | ||
| expect(getWords("one two three one").count).to eq 3 | ||
| end | ||
| end | ||
|
|
||
| describe 'No Special Chars' do | ||
| it 'No Special Chars' do | ||
| expect(getWords("one two! one")["two"]).to eq 1 | ||
| expect(getWords("one two. one")["two"]).to eq 1 | ||
| expect(getWords("one two- one")["two"]).to eq 1 | ||
| expect(getWords("one two? one")["two"]).to eq 1 | ||
| end | ||
| end | ||
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.
No need for an explicit return here.
With regards to method names, lower snakecase is generally the rule.
https://github.com/bbatsov/ruby-style-guide