Created
November 1, 2015 04:59
-
-
Save kangkyu/6680bb976689e2513b08 to your computer and use it in GitHub Desktop.
Today's Kata 10-30-2015
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
# today's code kata: Write a function called word_count, that takes a sentence as a string as input, and returns the frequency of each word in that sentence, as a hash whose key is the word, and whose count is the number of times that word appears in that sentence. | |
# 1) ignore case, 2) ignore any non word characters | |
# Here is the paragraph: | |
# The quick brown fox jumped over the moon. This mammal did not jump over the sun; it jumped over the moon. And it was quick about it. | |
# let's use the whole paragraph | |
def word_count(paragraph) | |
paragraph.split(/\W+/).map(&:downcase).each_with_object(Hash.new(0)) {|w, hsh| hsh[w] += 1 } | |
end | |
p word_count("The quick brown fox jumped over the moon. This mammal did not jump over the sun; it jumped over the moon. And it was quick about it.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment