Skip to content

About

Build a word frequency dictionary from a sentence

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Word Count

Premise

Write a function that takes a sentence as a string and returns a dictionary (object in JS) where each key is a word and each value is how many times that word appears in the sentence.

This exercise is about practicing two things together: iterating over a collection and building up a dictionary as you go.

Requirements

  • Words should be matched case-insensitively ("The" and "the" count as the same word).
  • Punctuation should not be part of the word. "hello," and "hello" count as the same word.
  • The returned dictionary's keys should all be lowercase.

Examples

word_count("the cat sat on the mat")
# -> {"the": 2, "cat": 1, "sat": 1, "on": 1, "mat": 1}

word_count("Hello, hello! HELLO?")
# -> {"hello": 3}

word_count("")
# -> {}

Test Your Code

Run the test file in each language folder:

  • Python: python word_count_tests.py
  • JS: node wordCountTests.js

Stretch

Once your tests pass, extend it: write a second function most_common(sentence) (Python) / mostCommon(sentence) (JS) that returns the single most-used word. If there's a tie, return any one of them.

Stuck? Have a code error? Use the "4 Before Me" debugging checklist to help you solve it!

About

Build a word frequency dictionary from a sentence

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages