diff --git a/recherche_sequentielle_python/recherche_sequentielle_1.py b/recherche_sequentielle_python/recherche_sequentielle_1.py new file mode 100644 index 0000000..33b9336 --- /dev/null +++ b/recherche_sequentielle_python/recherche_sequentielle_1.py @@ -0,0 +1,16 @@ +# recherche_sequentielle_1(x,t) +# """ Cette fonction renvoie l'indice (position) de la première +# occurrence d'un nombre entier x dans un tableau t non trié """ + +def recherche_sequentielle_1(x,t): + """ Cette fonction renvoie l'indice (position) de la première + occurrence d'un nombre entier x dans un tableau t non trié """ + i = 0 + while t[i] != x: + i = i + 1 + return i + 1 + + +t = [1,2,3,4,5,6,7,8,9,10] +x = 5 +print(recherche_sequentielle_1(x,t)) \ No newline at end of file diff --git a/recherche_sequentielle_python/recherche_sequentielle_2.py b/recherche_sequentielle_python/recherche_sequentielle_2.py new file mode 100644 index 0000000..006f54b --- /dev/null +++ b/recherche_sequentielle_python/recherche_sequentielle_2.py @@ -0,0 +1,19 @@ +# recherche_sequentielle_2(x,t) +# """ Cette fonction renvoie l'indice (position) de la première +# occurrence d'un nombre entier x dans un tableau t non trié ou -1 si x n'est pas présent """ + +def recherche_sequentielle_2(x,t): + """ Cette fonction renvoie l'indice (position) de la première + occurrence d'un nombre entier x dans un tableau t non trié ou -1 si x n'est pas présent """ + i = 0 + while i < len(t) and t[i] != x: + i = i + 1 + if i < len(t): + return i + else: + return -1 + + +t = [1,2,3,4,5,6,7,8,9,10] +x = 11 +print(recherche_sequentielle_2(x,t)) \ No newline at end of file diff --git a/recherche_sequentielle_python/recherche_sequentielle_3.py b/recherche_sequentielle_python/recherche_sequentielle_3.py new file mode 100644 index 0000000..2a5c7f5 --- /dev/null +++ b/recherche_sequentielle_python/recherche_sequentielle_3.py @@ -0,0 +1,16 @@ +# recherche_sequentielle_3(x,t) +# """ Cette fonction renvoie l'indice (position) d'un nombre entier x +# dans un tableau t non trié ou -1 si x n'est pas présent (en utilisant une boucle bornée)""" + +def recherche_sequentielle_3(x,t): + """ Cette fonction renvoie l'indice (position) d'un nombre entier x + dans un tableau t non trié ou -1 si x n'est pas présent (en utilisant une boucle bornée)""" + for i in range(len(t)): + if t[i] == x: + return i + return -1 + + +t = [1,2,3,4,5,6,7,8,9,10] +x = 4 +print(recherche_sequentielle_3(x,t)) \ No newline at end of file