

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Pure Functions
incomplete
2: Pure Function Review
incomplete
3: Reference vs. Value
incomplete
4: Pass by Reference Impurity
incomplete
5: Input and Output
incomplete
6: Should I I/O?
incomplete
7: No-Op
incomplete
8: Memoization
incomplete
9: Referential Transparency
incomplete
10: Pure Functions Practice
incomplete
11: Pure Functions Practice
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
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...
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:
document string.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:
memos dictionary.Here are the steps to follow: