We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Memoization

Memoization is a technical term that basically means caching (storing a copy of) the result of a computation so that we don't have to compute it again in the future. For example, take this simple function:

def add(x: int, y: int) -> int:
    return x + y

A call of add(5, 7) will always evaluate to 12. If you think about it, once we know that add(5, 7) can be replaced with 12, we can just store 12 in memory as the result value. Then, the next time we need to add(5, 7), we can look up the value instead of repeating a (potentially expensive) CPU operation.

The slower and more complex the function, the more memoization can help speed things up.

It's pronounced "memOization," not "memORization." This confused me for quite a while in college. I thought my professor just didn't speak goodly...

Assignment

Counting the words in a document can be slow, so we want to memoize it. Complete the word_count_memo function. It takes two inputs:

  1. A document string.
  2. A memos dictionary. The keys are full document strings, and the values are the word count of the document.

It should return a tuple of two values:

  1. The word count of the given document.
  2. An updated memos dictionary.

Here are the steps to follow: