From 550d435a2008fc0eef83cb0d30fdb53c32de264f Mon Sep 17 00:00:00 2001
From: Romain Vuillemot <romain.vuillemot@gmail.com>
Date: Thu, 8 Apr 2021 13:09:35 +0200
Subject: [PATCH] maj code exos

---
 TD01/code/arbre-parcours-profondeur.py       | 80 ++++++++++++++++++++
 TD01/code/bfs-tree.py                        |  1 +
 TD01/code/bfs.py                             |  1 +
 TD01/code/comprehension-list-filter.py       |  1 +
 TD01/code/fibo-comp.py                       | 14 ----
 TD01/code/fibo-mem.py                        | 13 +---
 TD01/code/fibo-rec.py                        | 11 +++
 TD01/code/graph-check-color.py               | 38 ++++++++++
 TD01/code/graph-labyrinthe.py                | 36 +++++++++
 TD01/code/{graphe_dico.py => graphe-dico.py} |  0
 TD01/code/graphe-liens.py                    | 23 ++++++
 TD01/code/graphe-matrice.py                  | 12 +++
 TD01/code/listes-fusion.py                   | 26 +++++++
 TD01/code/recherche-dico.py                  |  3 +-
 TD01/code/tri-stabilite.py                   |  4 +-
 15 files changed, 237 insertions(+), 26 deletions(-)
 create mode 100644 TD01/code/arbre-parcours-profondeur.py
 delete mode 100644 TD01/code/fibo-comp.py
 create mode 100644 TD01/code/fibo-rec.py
 create mode 100644 TD01/code/graph-check-color.py
 create mode 100644 TD01/code/graph-labyrinthe.py
 rename TD01/code/{graphe_dico.py => graphe-dico.py} (100%)
 create mode 100644 TD01/code/graphe-liens.py
 create mode 100644 TD01/code/graphe-matrice.py
 create mode 100644 TD01/code/listes-fusion.py

diff --git a/TD01/code/arbre-parcours-profondeur.py b/TD01/code/arbre-parcours-profondeur.py
new file mode 100644
index 0000000..32204ba
--- /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 8599a5d..46ef666 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 3fa8624..93dee77 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 95bff0a..61c32be 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 b92d2d7..0000000
--- 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 bfcd71c..0279ffe 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 0000000..96bb4aa
--- /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 0000000..57487f7
--- /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 0000000..fe72013
--- /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 0000000..f14d367
--- /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 0000000..d4fe76a
--- /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 0000000..9bf050b
--- /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 427a4bb..94c069d 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 3e4e78d..77fe07c 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
-- 
GitLab