Module 27: Practice Zone

Let's practice Linear Search and think about algorithm efficiency!

Identify Code Parts

This simple function performs a linear search. Click on the highlighted parts to identify their role.

def linear_search(data, target):
    for index in range(len(data)): # Check each item
        if data[index] == target:
            return index  # Found it! Return position
    return -1      # Not found after checking all

Identify all 4 key parts!

Predict the Steps

How many comparisons (checks) will linear search need to find the target (or know it's not there)? Enter your prediction. This relates to *efficiency*.

List:
[...]
Target: ?

Simple Code Check

Let's practice a related concept. Use Python's `in` operator to check if a value exists in a list.

Task: Write a single line of Python using the in operator to check if the number 42 is present in the list [10, 20, 42, 55]. Print the result (which will be `True` or `False`).
Output will appear here...

Conceptual Check-in

Think about how linear search works and its efficiency. Answer the questions below briefly.

Question 1: Best Case

When does linear search perform the *fastest* (best-case scenario)? Briefly describe.

Question 2: Worst Case

When does linear search perform the *slowest* (worst-case scenario)? Briefly describe.

Question 3: Sorted Data

Does linear search become faster if the list is already sorted? Why or why not?

Module 27 Practice Complete!

Great work exploring Linear Search! You've visualized it, analyzed its code, and thought about its efficiency.
Ready for more? Tackle the Advanced Practice for Module 27!