Last active
August 29, 2015 14:07
-
-
Save danielbonnell/d3a36b47712b614f01c5 to your computer and use it in GitHub Desktop.
Common Words Challenge
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 most_common(string) | |
counts = {} | |
words = string.downcase.tr(",.?!",'').split(' ') | |
words.uniq.each do |word| | |
counts[word] = 0 | |
end | |
words.each do |word| | |
counts[word] = string.scan(word).count | |
end | |
max_quantity = counts.values.max | |
max_words = counts.select { |k, v| v == max_quantity }.keys | |
max_words | |
end | |
#most_common('a short list of words with some words\n') #['words'] | |
#most_common('Words in a short, short words, lists of words!\n') #['words'] | |
#most_common('a short list of words with some short words in it\n') #['words', 'short'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment