easy +10 pts

Linear Search Implementation

Find the first occurrence of a target in a list, returning its index or -1 if missing.

Write a function `linear_search(arr, target)` that takes a list `arr` and a value `target`, and returns the index of the **first** occurrence of `target` in the list. If `target` is not present, return `-1`. You must implement the search manually using a loop (do not use `list.index()` or other built-in search methods). The function should work for lists of any length, including empty lists. The values in the list and the target can be of any comparable type (e.g., integers, strings). ### Function signature: ```python def linear_search(arr, target): ... ```

Constraints

- `0 <= len(arr) <= 10^5` - The list may contain any comparable data types (e.g., ints, strs). - The target must be compared using `==`. - Time complexity: O(n), Space complexity: O(1).

Example

```python
>>> linear_search([1, 2, 3, 2], 2)
1
>>> linear_search([1, 2, 3], 4)
-1
>>> linear_search([], 0)
-1
```
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Iterate over the list using a loop that tracks the index.
Return the index as soon as you find a match.
If the loop finishes without a match, return -1.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.