Skip to content

Instantly share code, notes, and snippets.

@ellisandrews
Last active August 20, 2020 04:09
Show Gist options
  • Save ellisandrews/eb23d86569c20c4594ab640a9aa3c5e5 to your computer and use it in GitHub Desktop.
Save ellisandrews/eb23d86569c20c4594ab640a9aa3c5e5 to your computer and use it in GitHub Desktop.
Linear time complexity
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