# better_linear_search.py
# This program uses a linear search of an array of size n for
# value x but unlike its predecessor linear_search.py it does
# not traverse the entire array regardless of value x being
# discovered. It simply returns the value immediately once
# the value of x in found in the array. If not found then it
# will just return text saying it wasnt found.
# by: Scott Gordon
things=["bike","house","cat","computer","hammer"]deflinear_search(array,n,x):foriinrange(1,n):ifarray[i]==x:returnireturn"Not found!"print(linear_search(things,5,"cat"))# prints the index of "cat" which is 2
print(linear_search(things,5,"hammer"))# prints the index of "hammer" which is 4
print(linear_search(things,5,"pasta"))# prints Not found!
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)