Aller au contenu

Ep 31

le document

l'algorithme

Issue de : 23-NSI-09⚓

EXERCICE 1⚓

Programmer la fonction multiplication, prenant en paramĂštres deux nombres entiers n1 et n2, et qui renvoie le produit de ces deux nombres.

Les seules opĂ©rations autorisĂ©es sont l’addition et la soustraction.

Exemples :

🐍 Script Python
>>> multiplication(3,5)
15
>>> multiplication(-4,-8)
32
>>> multiplication(-2,6)
-12
>>> multiplication(-2,0)
0

RĂ©ponse

Complétez le code ci-dessous

###
#Mettre votre code icibksl-nlbksl-nl



Solution

###
def multiplication(n1, n2):bksl-nl if n1 < 0:bksl-nl return -multiplication(-n1, n2)bksl-nl if n2 < 0:bksl-nl return -multiplication(n1, -n2)bksl-nl resultat = 0bksl-nl for py-und in range(n2):bksl-nl resultat += n1bksl-nl return resultatbksl-nlbksl-nltry:bksl-nl assert multiplication(3,5) == 15bksl-nl assert multiplication(-4,-8) == 32bksl-nl assert multiplication(-2,6) == -12bksl-nl assert multiplication(-2,0) == 0bksl-nl print('Tout semble correct 👍')bksl-nlbksl-nlexcept AssertionError as asser:bksl-nl print('Le rĂ©sultat de votre fonction n\'est pas conforme đŸ€”')bksl-nl



EXERCICE 2⚓

On s’intĂ©resse dans cet exercice Ă  la recherche dichotomique dans un tableau triĂ© d’entiers.

Compléter la fonction suivante en respectant la spécification.

🐍 Script Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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 = ... 
        if x == tab[m]:
            return ... 
        if x > tab[m]:
            debut = m + 1
        else:
            fin = ... 
    return ... 

Exemples :

🐍 Script Python
>>> dichotomie([15, 16, 18, 19, 23, 24, 28, 29, 31, 33],28)
True
>>> dichotomie([15, 16, 18, 19, 23, 24, 28, 29, 31, 33],27)
False
RĂ©ponse

Complétez le code ci-dessous

###
def dichotomie(tab, x):bksl-nl """bksl-nl tab : tableau d'entiers trié dans l'ordre croissantbksl-nl x : nombre entierbksl-nl La fonction renvoie True si tab contient x et False sinonbksl-nl """bksl-nl debut = 0bksl-nl fin = len(tab) - 1bksl-nl while debut <= fin:bksl-nl m = ... bksl-nl if x == tab[m]:bksl-nl return ... bksl-nl if x > tab[m]:bksl-nl debut = m + 1bksl-nl else:bksl-nl fin = ... bksl-nl return ... bksl-nl



Solution

###
def dichotomie(tab, x):bksl-nl """bksl-nl tab : tableau d'entiers triĂ© dans l'ordre croissantbksl-nl x : nombre entierbksl-nl La fonction renvoie True si tab contient x et False sinonbksl-nl """bksl-nl debut = 0bksl-nl fin = len(tab) - 1bksl-nl while debut <= fin:bksl-nl m = (debut + fin) // 2 bksl-nl if x == tab[m]:bksl-nl return True bksl-nl if x > tab[m]:bksl-nl debut = m + 1bksl-nl else:bksl-nl fin = m - 1 bksl-nl return False bksl-nltry:bksl-nl assert dichotomie([15, 16, 18, 19, 23, 24, 28, 29, 31, 33],28) == Truebksl-nl assert dichotomie([15, 16, 18, 19, 23, 24, 28, 29, 31, 33],27) == Falsebksl-nl print('Tout semble correct 👍')bksl-nlbksl-nlexcept AssertionError as asser:bksl-nl print('Le rĂ©sultat de votre fonction n\'est pas conforme đŸ€”')bksl-nl