Last active
February 1, 2017 00:07
-
-
Save ryanveroniwooff/7e9868c2a2a5a870dbd86c29fa0a5d97 to your computer and use it in GitHub Desktop.
Word Counting Algorithm - Utilizing the Hash
This file contains 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 count_words(s) | |
return nil if s.nil? || s.split.empty? | |
s.downcase.gsub(/[^a-z0-9'!\s-]/i, '').split(" ").each_with_object(Hash.new(0)){ |word,counts| counts[word] += 1 } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment