Where Each Word Appears

Python for AIHard+10 XP+10

Where Each Word Appears

You're building a tiny search index for a list of sentences. Sentences are numbered from 1.

Write word_index(sentences). It returns a dictionary of word → list of sentence numbers that contain it.

  • Split each sentence on spaces and treat words as lowercase.
  • Each sentence number appears at most once per word, even if the word is used twice in that sentence.
  • Each list of numbers goes from smallest to largest.
  • Words appear in the dictionary in the order they are first seen.

Examples

sentences = ["The cat sat", "the dog"]
returns {'the': [1, 2], 'cat': [1], 'sat': [1], 'dog': [2]}
sentences = []
returns {}

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run