cartagenae icon

Linear Search (finding duplicates)

cartagenae | PRO | 11/18/18 11:09:49 AM UTC | 0 ⭐ | 411 πŸ‘οΈ | Never ⏰ | []
text |

560 B

|

None

|

0 πŸ‘

/

0 πŸ‘Ž

# Search list and target value
tour_locations = [ "New York City", "Los Angeles", "Bangkok", "Istanbul", "London", "New York City", "Toronto"]
target_city = "New York City"
Β #Linear Search Algorithm
def linear_search(search_list, target_value):
  matches = []
  for idx in range(len(search_list)):
    if search_list[idx] == target_value:
      matches.append(idx)
  if matches:
    return matches
  else:
    raise ValueError("{0} not in list".format(target_value))
Β #Function call
tour_stops = linear_search(tour_locations, target_city)
print(tour_stops)

Comments