-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomSearch.py
75 lines (55 loc) · 1.99 KB
/
randomSearch.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import random
from random import randint
def random_search(list, element):
""" random search
Parametri:
list_ (list): lista di numeri interi
element (int): elemento da trovare
Valore di Ritorno:
int: indice dell'elemento all'interno dell'array, altrimenti se non è presente ritorna -1
"""
counter_list = [0] * len(list)
while not check_list_elements(counter_list):
random_index = random.randint(0, len(list) - 1)
if list[random_index] == element:
return random_index
else:
counter_list[random_index] = True
return -1
def las_vegas_algorithm(list_, element):
"""Las vegas algorithm
Parametri:
list_ (list): lista di numeri interi
element (int): elemento da trovare
Valore di Ritorno:
int: indice dell'elemento all'interno dell'array, altrimenti se non è presente ritorna -1
"""
randomIndex = randint(0, len(list_))
while list_[randomIndex] != element:
randomIndex = randint(0, len(list_))
return randomIndex
def search_MA(list_, element, n_iteration):
"""Algoritmo di montecarlo
Parametri:
list_ (list): lista di numeri interi
element (int): elemento da trovare
n_iteration (list): numero massimo di iterazioni
Valore di Ritorno:
int: indice dell'elemento all'interno dell'array, altrimenti se non è presente ritorna -1
"""
for i in range(0, n_iteration):
random_index = randint(0, len(list_))
if list_[random_index] == element:
return random_index
return -1
def check_list_elements(list):
"""Check if the list is made all by Trues
Arguments:
list {[bool]} -- list that stores old indexes choose by the random number
Returns:
bool -- if the list is made by all true returns true else false
"""
for i in range(0, len(list)):
if list[i] == False:
return False
return True