ENSAE Ecole Nationale de la Statistique et de l ... - xavierdupre.fr
ENSAE Ecole Nationale de la Statistique et de l ... - xavierdupre.fr
ENSAE Ecole Nationale de la Statistique et de l ... - xavierdupre.fr
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
1. Séances notées 6f = open (file, "r")mot = [] 5for l in f :mot.append ( l.rep<strong>la</strong>ce ("\n", "") )f.close ()r<strong>et</strong>urn motmot = lit_fichier ("td_note_texte.txt")print mot# question 2<strong>de</strong>f est_trie (mot) : 15for i in range (1, len (mot)) :if mot [i-1] > mot [i] :r<strong>et</strong>urn Falser<strong>et</strong>urn Tru<strong>et</strong>ri = est_trie (mot)print "liste triée ", tri# question 3<strong>de</strong>f cherche (mot, m) : 25for i in range (0, len (mot)) :if mot [i] == m :r<strong>et</strong>urn ir<strong>et</strong>urn -1print "mot ACHATS ", cherche (mot, "ACHATS")print "mot achats ", cherche (mot, "achats")# question 4un = cherche (mot, "UN") 35<strong>de</strong>ux = cherche (mot, "DEUX")print "recherche normale ", un, <strong>de</strong>uxprint "nombre d’itérations", un + <strong>de</strong>ux# question 5, 6, nbun <strong>et</strong> nb<strong>de</strong>ux contiennent le nombre <strong>de</strong> comparaisons 40<strong>de</strong>f cherche_dicho (mot, m) :a = 0b = len (mot)-1nb = 0while a < b : 45nb += 1p = (a+b)/2if mot [p] == m : r<strong>et</strong>urn p,nbelif mot [p] > m : b = p-1else : a = p+1 50r<strong>et</strong>urn -1,nbun,nbun = cherche_dicho (mot, "UN")<strong>de</strong>ux,nb<strong>de</strong>ux = cherche_dicho (mot, "DEUX")print "recherche dichotomique ", un, <strong>de</strong>ux 55print "nombre d’itérations ", nbun + nb<strong>de</strong>ux# question 7"""Lors d’une recherche simple, au pire, l’élément cherche sera 60en <strong>de</strong>rnière position, ce qui signifie n itérations pour le trouver.Le coût <strong>de</strong> <strong>la</strong> recherche simple est en O(n)."""# question 8 65"""Lors <strong>de</strong> <strong>la</strong> recherche dichotomique, à chaque itération, on divise par <strong>de</strong>uxl’ensemble dans lequel <strong>la</strong> recherche s’effectue,102030