TP2 Nom de certaines variables changés
Adieu 'already defined from outerscope'
This commit is contained in:
parent
dd91bd7d48
commit
9c47dc6091
|
@ -155,7 +155,6 @@ print(
|
|||
question(2)
|
||||
|
||||
print('>>> l = [\'a\', 1]')
|
||||
l = ['a', 1]
|
||||
print('>>> l.sort()')
|
||||
print('TypeError: unorderable types: int() < str()')
|
||||
|
||||
|
@ -173,9 +172,9 @@ question(1)
|
|||
def sort(chaine):
|
||||
# TODO Docstring
|
||||
# TODO On peut pas faire plus court ?
|
||||
l = list(chaine)
|
||||
l.sort()
|
||||
return ''.join(l)
|
||||
t = list(chaine)
|
||||
t.sort()
|
||||
return ''.join(t)
|
||||
|
||||
print('>>> sort(\'timoleon\')')
|
||||
print(sort('timoleon'))
|
||||
|
@ -378,12 +377,12 @@ question(3)
|
|||
|
||||
ANAGRAMMES = dict()
|
||||
|
||||
for i in LEXIQUE:
|
||||
k = cle(i)
|
||||
for m in LEXIQUE:
|
||||
k = cle(m)
|
||||
if not k in ANAGRAMMES:
|
||||
ANAGRAMMES[k] = [i]
|
||||
ANAGRAMMES[k] = [m]
|
||||
else:
|
||||
ANAGRAMMES[k] = ANAGRAMMES[k] + [i]
|
||||
ANAGRAMMES[k] = ANAGRAMMES[k] + [m]
|
||||
|
||||
|
||||
print('Le dictionnaire des anagrammes contient %d entrées' % len(ANAGRAMMES))
|
||||
|
@ -423,13 +422,13 @@ NB_TESTS = 30 # Mettre à 0 pour réduire l'attente
|
|||
import time
|
||||
|
||||
debut = time.time()
|
||||
for i in range(NB_TESTS):
|
||||
anagrammes(LEXIQUE[i])
|
||||
for t in range(NB_TESTS):
|
||||
anagrammes(LEXIQUE[t])
|
||||
temps1 = time.time() - debut
|
||||
|
||||
debut = time.time()
|
||||
for i in range(NB_TESTS):
|
||||
anagrammes2(LEXIQUE[i])
|
||||
for t in range(NB_TESTS):
|
||||
anagrammes2(LEXIQUE[t])
|
||||
temps2 = time.time() - debut
|
||||
|
||||
print('La première méthode a duré %s secondes, et la deuxième %s secondes.' % (temps1, temps2),
|
||||
|
@ -450,12 +449,12 @@ def liste_possibilites(arbre, precede):
|
|||
# TODO Docstring
|
||||
# {1: {3: None}, {4: None}, 2: None} ⇒ [[1, 3], [1, 4], 2]
|
||||
possibilites = []
|
||||
for i in arbre:
|
||||
if type(arbre[i]) == dict:
|
||||
for branche in arbre:
|
||||
if type(arbre[branche]) == dict:
|
||||
possibilites = possibilites + \
|
||||
liste_possibilites(arbre[i], precede + [i])
|
||||
elif arbre[i] == None:
|
||||
possibilites = possibilites + [precede + [i]]
|
||||
liste_possibilites(arbre[branche], precede + [branche])
|
||||
elif arbre[branche] == None:
|
||||
possibilites = possibilites + [precede + [branche]]
|
||||
return possibilites
|
||||
|
||||
|
||||
|
@ -488,10 +487,10 @@ question(1)
|
|||
|
||||
|
||||
def sauver_dico():
|
||||
f = open(ANAGRAMMES_FICHIER, 'w')
|
||||
fichier = open(ANAGRAMMES_FICHIER, 'w')
|
||||
for i in ANAGRAMMES:
|
||||
f.write(i + ':' + ':'.join(ANAGRAMMES[i]) + '\n')
|
||||
f.close()
|
||||
fichier.write(i + ':' + ':'.join(ANAGRAMMES[i]) + '\n')
|
||||
fichier.close()
|
||||
|
||||
question(2)
|
||||
sauver_dico()
|
||||
|
@ -506,8 +505,8 @@ question(3)
|
|||
|
||||
def charger_dico():
|
||||
ANAGRAMMES = dict()
|
||||
f = open(ANAGRAMMES_FICHIER, 'r')
|
||||
for l in f:
|
||||
decoupe = ':'.split(str(l))
|
||||
fichier = open(ANAGRAMMES_FICHIER, 'r')
|
||||
for ligne in fichier:
|
||||
decoupe = ':'.split(str(ligne))
|
||||
ANAGRAMMES[decoupe[0]] = decoupe[1:]
|
||||
f.close()
|
||||
fichier.close()
|
||||
|
|
Reference in a new issue