Step 1: Naive Search

Every substring-search engine starts with the obvious idea: try the pattern at every position.

Implement naive_search(text, pattern) that returns a list of every start index where pattern occurs inside text, scanning left to right.

Matches may overlap. For example, searching for aa inside aaaa yields indices [0, 1, 2] because the pattern starts at three overlapping positions.

Rules:

  • Compare the pattern against each window text[i : i + m].
  • Return an empty list when the pattern is empty or longer than the text.
  • Indices must be in increasing order.
Hints
Starter code
def naive_search(text, pattern):
    """Return all start indices where pattern occurs in text (overlaps allowed)."""
    # TODO: slide a window of len(pattern) across text and collect matches
    pass
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.