Chapter 4 · Quest 3 of 5 · 10 XP
Recall
🔎 Big memories need search. A simple way real agents do it: find the note that shares the most words with the question. Here's how to turn text into a set of words:
words = set("Where is my red hat?".lower().replace("?", "").split())
print(words){'where', 'is', 'my', 'red', 'hat'}A set is like a list with no repeats. a & b keeps only the words both sets share, and len() counts them.
Instructions
- Make
words(text)that returns the set of lowercase words, with?,.and,removed. - Make
best_fact(question, facts)that returns the fact sharing the most words with the question. - Print
best_fact("What is my cat called?", FACTS).
💡 Hint
Keep a best score, starting at 0. For each fact: score = len(words(question) & words(fact)). If score is bigger than the best so far, remember that fact.
bot.py
Terminal