Last active
August 20, 2020 04:09
-
-
Save ellisandrews/eb23d86569c20c4594ab640a9aa3c5e5 to your computer and use it in GitHub Desktop.
Linear time complexity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Any, List | |
def linear_search(list_: List[Any], target_value: Any) -> int: | |
""" | |
Perform a linear search of an input list for a target value. | |
Return the index of the target value in the list, or -1 if not found. | |
""" | |
# Iterate over each item in the list, checking whether it is the target value | |
for index, item in enumerate(list_): | |
if item == target_value: | |
return index | |
# Return a sentinel value if the target_value was not found in the list | |
return -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment