diff --git a/TD01/code/arbre-parcours-profondeur.py b/TD01/code/arbre-parcours-profondeur.py new file mode 100644 index 0000000000000000000000000000000000000000..32204ba27d95aacccb35cc57c4fb987594d313f3 --- /dev/null +++ b/TD01/code/arbre-parcours-profondeur.py @@ -0,0 +1,80 @@ +class Noeud: + def __init__(self, v, c = []): + self.v = v + self.c = c + +def parcour_largeur(n): + + stack = [] + stack.append(n) + + while len(stack) > 0: + current = stack.pop(0) + + for s in current.c: + stack.append(s) + + print(current.v) + +def parcour_largeur(n): + + stack = [] + stack.append(n) + + while len(stack) > 0: + current = stack.pop(0) + + for s in current.c: + stack.append(s) + + print(current.v) + +def parcour_profondeur(n): + + stack = [] + stack.append(n) + + while len(stack) > 0: + current = stack.pop() + + for s in reversed(current.c): + stack.append(s) + + print(current.v) + +def parcour_profondeur_rec(n): + + if len(n.c) > 0: + parcour_profondeur_rec(n.c[0]) + + print(n.v) + + if len(n.c) > 1: + parcour_profondeur_rec(n.c[1]) + +def parcour_profondeur_rec_dic(n, A): + + if len(A[n]) > 0: + parcour_profondeur_rec_dic(A[n][0], A) + + print(n) + + if len(A[n]) > 1: + parcour_profondeur_rec_dic(A[n][1], A) + + +racine = Noeud("chien", [Noeud("petit", [Noeud("le")]), Noeud("et", [Noeud("jaune"), Noeud("noir")])]) + +parcour_profondeur_rec(racine) + +A = {"chien": ["petit", "et"], + "petit": ["le"], + "le": [], + "et": ["jaune", "noir"], + "jaune": [], + "noir": [] +} + +parcour_profondeur_rec_dic("chien", A) + +print(A.keys()) \ No newline at end of file diff --git a/TD01/code/bfs-tree.py b/TD01/code/bfs-tree.py index 8599a5d6c13e998d4588dbebcbc318244ea21e74..46ef6668760d6867674532439748872392f2e495 100644 --- a/TD01/code/bfs-tree.py +++ b/TD01/code/bfs-tree.py @@ -1,4 +1,5 @@ import collections + class graph: def __init__(self,gdict=None): if gdict is None: diff --git a/TD01/code/bfs.py b/TD01/code/bfs.py index 3fa862474018357a6ea7cb0bb4709975a28e65e1..93dee777faa4dfb41480912ee9f7695664557630 100644 --- a/TD01/code/bfs.py +++ b/TD01/code/bfs.py @@ -1,4 +1,5 @@ import collections + class graph: def __init__(self,gdict=None): if gdict is None: diff --git a/TD01/code/comprehension-list-filter.py b/TD01/code/comprehension-list-filter.py index 95bff0ab8925e58543116ffd5ac47e0dc988f6ca..61c32be717307d12a8a21d9b73b4e398d2089bb1 100755 --- a/TD01/code/comprehension-list-filter.py +++ b/TD01/code/comprehension-list-filter.py @@ -1,4 +1,5 @@ carres = [] + for x in range(10): if x % 2: carres.append(x * x) diff --git a/TD01/code/fibo-comp.py b/TD01/code/fibo-comp.py deleted file mode 100644 index b92d2d78044d71238ab0095a6ad7f42d0cf4744d..0000000000000000000000000000000000000000 --- a/TD01/code/fibo-comp.py +++ /dev/null @@ -1,14 +0,0 @@ -def fibo1(f2, f1, i): - if i > 0: - return fibo1(f1, f2+f1, i-1) - else: - return f2 - - -def fibo2(f2, f1, i): - while i > 0: - f2, f1, i = f1, f2+f1, i-1 - return f2 - -print(fibo1(6)) -print(fibo2(6)) \ No newline at end of file diff --git a/TD01/code/fibo-mem.py b/TD01/code/fibo-mem.py index bfcd71c4e9f7263d1fe04b49914b3b5ee39c709d..0279ffe63bdd804d3a199029360e811bf5ecf580 100644 --- a/TD01/code/fibo-mem.py +++ b/TD01/code/fibo-mem.py @@ -10,14 +10,9 @@ def fib(n, lookup): # On renvoie la n-eme valeur return lookup[n] - -def main(): - n = 6 - max = 100 - # Initialise la table de cache - lookup = [None]*(max) - print("Le nombre de fibonacci est ", fib(n, lookup)) - # Le nombre de fibonacci est 8 if __name__=="__main__": - main() \ No newline at end of file + n = 6 + max = 100 + lookup = [None]*(max) # initialise la table de cache + print("Le nombre de fibonacci est ", fib(n, lookup)) # affiche 8 \ No newline at end of file diff --git a/TD01/code/fibo-rec.py b/TD01/code/fibo-rec.py new file mode 100644 index 0000000000000000000000000000000000000000..96bb4aa3356b4c45b96deaa318330d28445aaad9 --- /dev/null +++ b/TD01/code/fibo-rec.py @@ -0,0 +1,11 @@ +def fibo(n): + if n <= 1: + return n + else: + return(fibo(n-1) + fibo(n-2)) + +if __name__=="__main__": + n = 10 + print("Séquence de Fibonacci de {} nombres :".format(n)) + for i in range(10): + print(fibo(i)) \ No newline at end of file diff --git a/TD01/code/graph-check-color.py b/TD01/code/graph-check-color.py new file mode 100644 index 0000000000000000000000000000000000000000..57487f7d18dcc8aac7d58b437a214b4f01dad93a --- /dev/null +++ b/TD01/code/graph-check-color.py @@ -0,0 +1,38 @@ +class Node: + def __init__(self, v = None, n = []): + self.v = v + self.n = n + self.visited = False + +def verifier(r): + + stack = [r] + + while len(stack) > 0: + c = stack.pop(0) + for n in c.n: + if(c.v == n.v): # meme couleur + print("deux voisins de meme couleur") + return False + if not n.visited: + stack.append(n) + n.visited = True + + return True + +if __name__=="__main__": + n1 = Node("gray") + n2 = Node("black") + n3 = Node("gray") + n4 = Node("gray") + n5 = Node("black") + n6 = Node("gray") + + n1.n = [n2] + n2.n = [n1, n3, n4] + n3.n = [n2, n5] + n4.n = [n2, n5] + n5.n = [n3, n4, n6] + n6.n = [n5] + + print(verifier(n1)) # True \ No newline at end of file diff --git a/TD01/code/graph-labyrinthe.py b/TD01/code/graph-labyrinthe.py new file mode 100644 index 0000000000000000000000000000000000000000..fe720135b14febe1a4663e25f5bfa72c94f83826 --- /dev/null +++ b/TD01/code/graph-labyrinthe.py @@ -0,0 +1,36 @@ +labyrinthe = [[0, 1, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 1, 0, 1, 1, 1, 1, 1, 1, 0], + [0, 1, 0, 1, 0, 0, 0, 0, 1, 0], + [0, 1, 0, 1, 0, 1, 1, 0, 1, 0], + [0, 0, 0, 1, 0, 1, 1, 0, 1, 0], + [0, 1, 0, 1, 0, 0, 1, 0, 1, 0], + [0, 1, 0, 1, 1, 1, 1, 0, 1, 0], + [0, 1, 0, 0, 0, 0, 0, 0, 1, 0], + [0, 1, 1, 1, 0, 1, 1, 1, 1, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] + +def affiche_labyrinthe (l): + print('\n'.join(''.join('#' if item else '.' for item in row) for row in l)) + print() + +def voisins(l, x, y): + return ((x+dx, y+dy) + for dx, dy in ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)) + if 0 <= x+dx < len(l[0]) and 0 <= y+dy < len(l) and l[y+dy][x+dx] == 0) + +# IMPORTANT : chaque appel récursif ne doit pas modifier les chemins des autres, +# donc chacun a une copie non modifiable du chemin, ce pourquoi on utilise un tuple plutôt que liste +def existe_profondeur(l, x0=0, y0=0, chemin=()): + # print(x0, y0) + if (x0, y0) == (len(l[0])-1, len(l)-1): # condition de terminaison + return True + chemin += ((x0, y0),) # on crée un nouveau chemin avec la position courante + for x, y in voisins(l, x0, y0): + if (x, y) in chemin: # on ignore le voisin si déjà visité par le chemin courant + continue + if existe_profondeur(l, x, y, chemin): # appel récursif à partir de la position voisine + return True # on a trouvé un chemin par la position voisine, donc aussi par l'actuelle + return False # aucun des voisins ne mène à la sortie, donc l'actuelle non plus + +affiche_labyrinthe(labyrinthe) +print(existe_profondeur(labyrinthe)) diff --git a/TD01/code/graphe_dico.py b/TD01/code/graphe-dico.py similarity index 100% rename from TD01/code/graphe_dico.py rename to TD01/code/graphe-dico.py diff --git a/TD01/code/graphe-liens.py b/TD01/code/graphe-liens.py new file mode 100644 index 0000000000000000000000000000000000000000..f14d367343a20b7fdbf293523ca2ade83d133212 --- /dev/null +++ b/TD01/code/graphe-liens.py @@ -0,0 +1,23 @@ +class Graphe: + def __init__(self, *args): + self.edges = [e for e in args] + self.nodes = [] + for e in self.edges: + if e[0] not in self.nodes: + self.nodes += [ e[0] ] + if e[1] not in self.nodes: + self.nodes += [ e[1] ] + + def mat(self): + self.mat = [[ 0 for j in range(len(self.nodes))] for i in range(len(self.nodes))] + for i in self.edges: + self.mat[ self.nodes.index(i[0]) ][ self.nodes.index(i[1]) ] = 1 + self.mat[ self.nodes.index(i[1]) ][ self.nodes.index(i[0]) ] = 1 + + return self.mat + +if __name__ == "__main__": + G = Graphe( ('A','B') , ('A','C') , ('B','C') ) + print( 'Les noeuds de G sont : {}'.format(G.nodes) ) + print( 'Les arêtes de G sont : {}'.format(G.edges) ) + print( 'La matrice d\'adjacence de G est : {}'.format(G.mat()) ) \ No newline at end of file diff --git a/TD01/code/graphe-matrice.py b/TD01/code/graphe-matrice.py new file mode 100644 index 0000000000000000000000000000000000000000..d4fe76afc3e5d12e2ee9330818f59f4e9dc5e1ad --- /dev/null +++ b/TD01/code/graphe-matrice.py @@ -0,0 +1,12 @@ + +# Création d'un matrice d'adjacence pour un graphe +n = 5 +G = [[0 for j in range(n)] for i in range (n)] + +# Ajout d'un lien (si le graphe est non orienté) +G[0][1] = 1 +G[1][0] = 1 + +# Affichage du graphe +for row in G: + print(row) \ No newline at end of file diff --git a/TD01/code/listes-fusion.py b/TD01/code/listes-fusion.py new file mode 100644 index 0000000000000000000000000000000000000000..9bf050b5f2fbf846174904c14fdf17d4b8afce14 --- /dev/null +++ b/TD01/code/listes-fusion.py @@ -0,0 +1,26 @@ +def fusion(l1, l2): + + res = [] + i = 0 + j = 0 + + while i < len(l1) and j < len(l2): + if l1[i] < l2[j]: + res.append(l1[i]) + i += 1 + else: + res.append(l2[i]) + j += 1 + + while i < len(l1): + res.append(l1[i]) + i += 1 + + while j < len(l2): + res.append(l2[j]) + j += 1 + + return res + +print(fusion([2, 2, 3], [0, 4, 5, 14, 20, 25])) +# [0, 2, 2, 3, 4, 5, 14, 20, 25] \ No newline at end of file diff --git a/TD01/code/recherche-dico.py b/TD01/code/recherche-dico.py index 427a4bb84aa0a176de669abc9c0278e75481bfe9..94c069d97a668310a5b354cc29cc5af6f9cd4071 100644 --- a/TD01/code/recherche-dico.py +++ b/TD01/code/recherche-dico.py @@ -13,4 +13,5 @@ def recherche(element, liste): return a -print (recherche(3, [3, 1, 4, 2, 3])) +if __name__=="__main__": + print (recherche(3, [3, 1, 4, 2, 3])) diff --git a/TD01/code/tri-stabilite.py b/TD01/code/tri-stabilite.py index 3e4e78d291e53d2fe601575e28db8e3caa3614f8..77fe07c3b0599888829432dbf0c7d0da7990642f 100644 --- a/TD01/code/tri-stabilite.py +++ b/TD01/code/tri-stabilite.py @@ -13,5 +13,5 @@ def algo(words): return list(res) - -print(algo(["Pierre", "Jean", "Marie", "Eric", "Nathalie", "Yvonne"])) \ No newline at end of file +if __name__=="__main__": + print(algo(["Pierre", "Jean", "Marie", "Eric", "Nathalie", "Yvonne"])) \ No newline at end of file