Advertisement
cartagenae

Linear Search (finding duplicates)

Nov 18th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. # Search list and target value
  2. tour_locations = [ "New York City", "Los Angeles", "Bangkok", "Istanbul", "London", "New York City", "Toronto"]
  3. target_city = "New York City"
  4.  
  5. #Linear Search Algorithm
  6. def linear_search(search_list, target_value):
  7. matches = []
  8. for idx in range(len(search_list)):
  9. if search_list[idx] == target_value:
  10. matches.append(idx)
  11. if matches:
  12. return matches
  13. else:
  14. raise ValueError("{0} not in list".format(target_value))
  15.  
  16. #Function call
  17. tour_stops = linear_search(tour_locations, target_city)
  18. print(tour_stops)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement