nsi algo Flashcards

1
Q

Test de liste vide

A

def vide(liste:list):
“"”renvoie True si la liste est vide et False sinon”””
if len(liste) == 0:
return True
else:
return False

L1 = []
L2 = [3, 5, 9]
assert vide(L1) == True
assert vide(L2) == False

True
False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Minimum d’une liste

A

def minimum(liste):
“”” renvoie le minimum d’une liste”””
m = float(‘inf’)
for x in liste:
if x < m:
m = x
return m

ma_liste = [7, 14, 6, 49, 5, 2, 84, 36, 12, 27]
print(minimum(ma_liste))
assert minimum(ma_liste) == 2

2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Retrait d’un élément

A

def enlever(liste:list, x:float):
“"”supprime l’élement x de la liste”””
liste.remove(x)

ma_liste = [7, 14, 6, 49, 5, 2, 84, 36, 12, 27]
assert ma_liste == [7, 14, 6, 49, 5, 2, 84, 36, 12, 27]
print(ma_liste)
enlever(ma_liste, 14)
assert ma_liste == [7, 6, 49, 5, 2, 84, 36, 12, 27]
print(ma_liste)

  • [7, 14, 6, 49, 5, 2, 84, 36, 12, 27]*
  • [7, 6, 49, 5, 2, 84, 36, 12, 27] *
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Ajout d’un élément

A

def ajouter(liste:list,x:float):
liste.append (x)

ma_liste = [7, 6, 49, 5, 2, 84, 36, 12, 27]
assert ma_liste == [7, 6, 49, 5, 2, 84, 36, 12, 27]
ajouter(ma_liste, 14)
assert ma_liste == [7, 6, 49, 5, 2, 84, 36, 12, 27, 14]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Sélection tris

A

def tri_selection(liste_a_trier):
“"”renvoie un liste triée en appliquant le tri par sélection à partir d’une liste à trier”””
liste_triee = []
while not vide(liste_a_trier):
# à compléter
n = minimum(liste_a_trier)
enlever(liste_a_trier,n)
ajouter(liste_triee,n)
return liste_triee

liste = [7, 14, 6, 49, 5, 2, 84, 36, 12, 27]
print(liste)
liste_triee = tri_selection(liste)
print(liste)
print(liste_triee)

[7, 14, 6, 49, 5, 2, 84, 36, 12, 27]
[]
[2, 5, 6, 7, 12, 14, 27, 36, 49, 84]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Insertion

A

def inserer(liste:list,x:float):
“"”insère l’élément x à la bonne position dans une liste supposée triée”””
for position in range(len(liste)):
if liste[position]>x:
break
liste.insert(position, x)

ma_liste = [2, 5, 6, 7, 12, 14, 27, 36, 49, 84]
print(ma_liste)
inserer(ma_liste, 25)
print(ma_liste)

  • [2, 5, 6, 7, 12, 14, 27, 36, 49, 84]
    [2, 5, 6, 7, 12, 14, 25, 27, 36, 49, 84] *
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Tri par insertion

A

def tri_insertion(liste_à_trier):
“"”renvoie un liste triée en appliquant le tri par insertion à partir d’une liste à trier”””
# liste triée initialisée à [∞]
liste_triee = [float(‘inf’)]
while not vide(liste_à_trier):
n = liste_à_trier.pop()
inserer(liste_triee,n)
# pour enlever ∞
liste_triee.pop()
return liste_triee

liste = [7,14,6,49,5,2,84,36,12,27]
print(liste)
trie = tri_insertion(liste)
print(liste)
print(trie)

[7, 14, 6, 49, 5, 2, 84, 36, 12, 27]
[]
[2, 5, 6, 7, 12, 14, 27, 36, 49, 84]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Coût

A

from time import *

T = liste_aléatoire(100)
time1 = perf_counter()
res = tri_selection(T)
time2 = perf_counter()
dt = 1000*(time2 - time1) #m/s
print(dt)

0.42955949902534485

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

enlever elem dico

A

{“Ernoult”:290,
“Lebrun”:1050,
“Lematte”:491,
“Bariseau”:358,
“Faveeuw”:630,
“Lorthioir”:320,
“Ansart”:410,
“Bechara”:1178}

def enlever(dico:dict,k):
del dico[“Ernoult”]

{‘Lebrun’: 1050,
‘Lematte’: 491,
‘Bariseau’: 358,
‘Faveeuw’: 630,
‘Lorthioir’: 320,
‘Ansart’: 410,
‘Bechara’: 1178}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

ajouter elem dico

A

def ajouter(dico:dict,eleve:tuple):
# eleve est un tuple(key,valeur)

dico[eleve[0]] = eleve[1]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly