Correction de l'épreuve 9
Sujet (PDF intégré)
Exercice 1
def multiplication(n1, n2):
# on se ramène d'abord au cas où n1 et n2 sont tous les deux positifs :
if n1 < 0:
return -multiplication(-n1, n2)
if n2 < 0:
return -multiplication(n1, -n2)
resultat = 0
for _ in range(n2):
resultat += n1
return resultat
Exercice 2
def dichotomie(tab, x):
"""
tab : tableau d’entiers trié dans l’ordre croissant
x : nombre entier
La fonction renvoie True si tab contient x et False sinon
"""
debut = 0
fin = len(tab) - 1
while debut <= fin:
m = (debut + fin) // 2
if x == tab[m]:
return True
if x > tab[m]:
debut = m + 1
else:
fin = m - 1
return False
Quelques petits rappels de Première
Précédent | Suivant