Skip to content

Instantly share code, notes, and snippets.

@kangkyu
Created November 1, 2015 04:59
Show Gist options
  • Save kangkyu/6680bb976689e2513b08 to your computer and use it in GitHub Desktop.
Save kangkyu/6680bb976689e2513b08 to your computer and use it in GitHub Desktop.
Today's Kata 10-30-2015
# 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