Skip to content

Instantly share code, notes, and snippets.

@danielbonnell
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielbonnell/d3a36b47712b614f01c5 to your computer and use it in GitHub Desktop.
Save danielbonnell/d3a36b47712b614f01c5 to your computer and use it in GitHub Desktop.
Common Words Challenge
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