Advertisement
kevinbocky

linear_search_functions.py

Jan 28th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #linear search functions
  2. numbers = [1,2,5,8,7,9,12,51,41,87,95,98,992,52,472,21,43]
  3. numbers2 = [2,5,8,18,51,45,65,118,781,341,586,981,23,46,93]
  4.  
  5.  
  6. def search1(list_to_search,find): #linear search doing while loop
  7. position = 0
  8. while position < len(list_to_search):
  9. if find == (list_to_search[position]): #compares (find) to a specific position in (list_to_search)
  10. print("object " + str(find) + " is at position " + str(position + 1))
  11. position = len(list_to_search)
  12.  
  13.  
  14. elif (position + 1) == len(list_to_search):
  15. print("Not found")
  16. position = position + 1
  17.  
  18. else:
  19. position = position + 1
  20.  
  21.  
  22.  
  23.  
  24. search1(numbers,43)
  25.  
  26.  
  27.  
  28. def search2(list_to_search,find): #linear search with for loop
  29. for position in range(len(list_to_search)):
  30. if find == (list_to_search[position]): #compares (find) to a specific position in (list_to_search)
  31. print("object " + str(find) + " is at position " + str(position + 1))
  32. position = len(list_to_search)
  33.  
  34. elif (position + 1) == len(list_to_search):
  35. print("Not found")
  36. position = position + 1
  37.  
  38.  
  39.  
  40.  
  41. search2(numbers2,93)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement