Skip to content
Snippets Groups Projects
Commit 550d435a authored by Romain Vuillemot's avatar Romain Vuillemot
Browse files

maj code exos

parent 4527a199
Branches
No related tags found
Loading
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
import collections import collections
class graph: class graph:
def __init__(self,gdict=None): def __init__(self,gdict=None):
if gdict is None: if gdict is None:
......
import collections import collections
class graph: class graph:
def __init__(self,gdict=None): def __init__(self,gdict=None):
if gdict is None: if gdict is None:
......
carres = [] carres = []
for x in range(10): for x in range(10):
if x % 2: if x % 2:
carres.append(x * x) carres.append(x * x)
......
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
...@@ -11,13 +11,8 @@ def fib(n, lookup): ...@@ -11,13 +11,8 @@ def fib(n, lookup):
# On renvoie la n-eme valeur # On renvoie la n-eme valeur
return lookup[n] return lookup[n]
def main(): if __name__=="__main__":
n = 6 n = 6
max = 100 max = 100
# Initialise la table de cache lookup = [None]*(max) # initialise la table de cache
lookup = [None]*(max) print("Le nombre de fibonacci est ", fib(n, lookup)) # affiche 8
print("Le nombre de fibonacci est ", fib(n, lookup)) \ No newline at end of file
# Le nombre de fibonacci est 8
if __name__=="__main__":
main()
\ No newline at end of file
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
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
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))
File moved
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
# 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
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
...@@ -13,4 +13,5 @@ def recherche(element, liste): ...@@ -13,4 +13,5 @@ def recherche(element, liste):
return a return a
if __name__=="__main__":
print (recherche(3, [3, 1, 4, 2, 3])) print (recherche(3, [3, 1, 4, 2, 3]))
...@@ -13,5 +13,5 @@ def algo(words): ...@@ -13,5 +13,5 @@ def algo(words):
return list(res) return list(res)
if __name__=="__main__":
print(algo(["Pierre", "Jean", "Marie", "Eric", "Nathalie", "Yvonne"])) print(algo(["Pierre", "Jean", "Marie", "Eric", "Nathalie", "Yvonne"]))
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment