0%
⭐ 0 XPMap
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

  1. Make words(text) that returns the set of lowercase words, with ?, . and , removed.
  2. Make best_fact(question, facts) that returns the fact sharing the most words with the question.
  3. 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.

Ask the community

bot.py
Terminal
← BackNext →

Chapter 4: Memory

The full quest map

Talk it out

  1. Read the instruction with the ✗ out loud.
  2. Say what your code does, line by line, as if to a friend.
  3. Find the line where what it does and what you want split apart.
  4. Press Run and read the terminal. Is that what you expected?
  5. Still stuck? Press Ask Pip why, or peek at the solution.
  6. Or ask on the community help board: say which quest, and paste your code.
New badge!

Your bot just got a new part. See it on the quest map.

Name your bot

It levels up as you play, and gets a new part with every chapter.