-
Notifications
You must be signed in to change notification settings - Fork 36
Sam's solution #21
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
Open
sampriddy
wants to merge
5
commits into
paircolumbus:master
Choose a base branch
from
sampriddy:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Sam's solution #21
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
12bc942
Initial solution
5f7e92a
Move logic into functions with reasonable names
165e1f4
Move methods into a class
9b0a473
Fix problem with regex (failed to find one-letter words)
cf3aff3
Change WORDS_REGEX from class variable to constant in class scope
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| TEXT = File.read('speech.txt') | ||
|
|
||
|
|
||
| class WordCounter | ||
| @@WORDS_REGEX = /\w+[a-z']*/ | ||
|
|
||
| def self.solve text | ||
| words = find_words TEXT | ||
| word_freqs = count_words words | ||
| sorted_words = sort_words word_freqs | ||
| print_sorted_word_counts sorted_words | ||
| end | ||
|
|
||
| def self.find_words text | ||
| text.downcase.scan @@WORDS_REGEX | ||
| end | ||
|
|
||
| def self.count_words words_array | ||
| Hash.new(0).tap do |word_count| | ||
| words_array.each { |word| word_count[word] += 1 } | ||
| end | ||
| end | ||
|
|
||
| def self.sort_words words_frequency_hash | ||
| words_frequency_hash.sort_by { |word, count| -count } | ||
| end | ||
|
|
||
| def self.wordcount_print_format word | ||
| "#{word[1]} - #{word[0].capitalize}" | ||
| end | ||
|
|
||
| def self.print_sorted_word_counts sorted_words | ||
| sorted_words.each { |word| puts wordcount_print_format(word) } | ||
| end | ||
| end | ||
|
|
||
|
|
||
| WordCounter.solve(TEXT) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Class instance variables are generally frowned upon. https://github.com/bbatsov/ruby-style-guide#no-class-vars
This should work fine as a constant instead. (Drop the "@@".)
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.
Wow, had no idea that a child's class variable OVERWRITES a class variable of the same name within its superclass. That's pretty surprising and now I see why class vars are not favored