# linear_search.py
# This program uses a linear search of an array of size n for
# value x
# by: Scott Gordon
things=["bike","house","cat","computer","hammer"]deflinear_search(array,n,x):answer="Not found!"foriinrange(1,n):ifarray[i]==x:answer=ireturnanswerprint(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!
yeah, i had added that to the better linear search algo after this one. And yes using len(array) is more Pythonic but I was trying to write it as close to psuedocode as possible while still using Python.
Thanks for the suggestions though!
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (2)
Why not this? It break the loop when find the element, there is another way by getting the length of the array too. :D
yeah, i had added that to the better linear search algo after this one. And yes using len(array) is more Pythonic but I was trying to write it as close to psuedocode as possible while still using Python.
Thanks for the suggestions though!