Ms. Whitby's Classroom logo Ms. Whitby's Classroom

Algorithms & Testing — Lesson 3: Find the first matching position

New skill: Linear search and enumerate
Previously learned: Units 01–09

Why this matters

Search underlies app menus, student rosters, and debugging lists of events.

Step 1 — Explore working Python

Read the program below. Before running it, write down the result you expect.

names = ['Ava', 'Kai', 'Ava']
for index, name in enumerate(names):
    if name == 'Kai':
        print(index)
        break

Step 2 — Trace the code

  1. Use enumerate() when you need both the index and the value.
  2. Compare each item with the target using ==.
  3. Return immediately on a match, otherwise return -1 after the loop.

Step 3 — Check your understanding

What index should a search return for the first 'Ava'? What if the name is missing?

Step 4 — Watch for this mistake

A return -1 placed inside the loop ends the search after checking only the first item.

Practice: Find the first matching position

Complete first_position(values, target) to return the index of the first equal value, or -1 when not found.

Function contract: first_position(values, target)
Starter file: main.py

Work in the editor, using Run to explore and Check to compare against the published examples. Your submitted code, not the public check, determines the recorded score.

Hint 1 — Start Loop using `enumerate(values)`.
Hint 2 — Build Return the index as soon as the value matches.
Hint 3 — Test Return -1 after checking every item without a match.

Reflect

In 3–5 sentences, explain the algorithm you chose, one test case you predicted, and how you would detect an incorrect answer.

Optional reading

For another perspective on this skill, see How to Think Like a Computer Scientist (interactive textbook). This Playground lesson, worked example, and exercise are original; no textbook passages were reproduced.