Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save liuzheng1990/e7224e0c3017ec55b300cb8877106789 to your computer and use it in GitHub Desktop.
Save liuzheng1990/e7224e0c3017ec55b300cb8877106789 to your computer and use it in GitHub Desktop.
def linear_search(l, target, upper_limit=None):
"""Search ``l`` (up to the ``upper_limit`` element
if specified) for ``target``. Return a boolean indicating
whether ``target`` is found in ``l`` or not.
"""
for idx, item in enumerate(l):
if item == target:
return True
if upper_limit is not None and idx >= upper_limit:
break
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment