Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #linear search functions
- numbers = [1,2,5,8,7,9,12,51,41,87,95,98,992,52,472,21,43]
- numbers2 = [2,5,8,18,51,45,65,118,781,341,586,981,23,46,93]
- def search1(list_to_search,find): #linear search doing while loop
- position = 0
- while position < len(list_to_search):
- if find == (list_to_search[position]): #compares (find) to a specific position in (list_to_search)
- print("object " + str(find) + " is at position " + str(position + 1))
- position = len(list_to_search)
- elif (position + 1) == len(list_to_search):
- print("Not found")
- position = position + 1
- else:
- position = position + 1
- search1(numbers,43)
- def search2(list_to_search,find): #linear search with for loop
- for position in range(len(list_to_search)):
- if find == (list_to_search[position]): #compares (find) to a specific position in (list_to_search)
- print("object " + str(find) + " is at position " + str(position + 1))
- position = len(list_to_search)
- elif (position + 1) == len(list_to_search):
- print("Not found")
- position = position + 1
- search2(numbers2,93)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement