forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_search_sentinel.py
32 lines (26 loc) · 989 Bytes
/
linear_search_sentinel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
""" Implementacao do algoritmo de busca sentinela """
def busca_sentinela(list_to_search, value):
"""
Implementacao de um algoritmo de busca sentinela.
Argumentos:
value: Any. Valor a ser buscado na lista
list_to_search: list. lista na qual o valor sera buscado
Retorna o indice do valor em "list_to_search" ou -1 caso nao exista nela.
"""
list_to_search.append(value)
list_index = 0
while list_to_search[list_index] != value:
list_index = list_index + 1
list_to_search.pop()
if list_index == len(list_to_search):
return -1
return list_index
if __name__ == "__main__":
some_list = [1, 9, 2, 8, 7, 4, 5, 6, 4, 3, 10, 0]
NUMBER_TO_FIND = 4
NUMBER_INDEX = busca_sentinela(some_list, NUMBER_TO_FIND)
print(some_list)
if NUMBER_INDEX >= 0:
print("Found value {} at position {}.".format(NUMBER_TO_FIND, NUMBER_INDEX))
else:
print("Could not find value {}.".format(NUMBER_TO_FIND))