From 5dbc3e3559712103f0b87b641bfaebecec0e6cee Mon Sep 17 00:00:00 2001 From: Romain Vuillemot <romain.vuillemot@gmail.com> Date: Thu, 25 Mar 2021 21:56:53 +0100 Subject: [PATCH] Formattage code exemples --- TD01/code/arbre_k_aire.py | 43 ++++ TD01/code/bfs-tree.py | 29 +++ TD01/code/bfs.py | 31 +++ TD01/code/dfs-arbre.py | 17 ++ TD01/code/graph-cycle.py | 26 ++ TD01/code/graph-get.py | 25 ++ TD01/code/graph-parcours-bellman.py | 20 ++ TD01/code/graph-parcours-dijkstra.py | 43 ++++ TD01/code/graph-similarite.py | 37 +++ TD01/code/listes-liees-boucle.py | 15 ++ TD01/code/listes.py | 5 - TD01/code/{addition.py => math-addition.py} | 0 TD01/code/{division.py => math-division.py} | 0 TD01/code/{moyenne.py => math-moyenne.py} | 0 TD01/code/math-multiplication-rec.py | 8 + TD01/code/{parite.py => math-parite.py} | 0 TD01/code/{premier.py => math-premier.py} | 0 TD01/code/{racines.py => math-racines.py} | 0 TD01/code/recherche2.py | 23 ++ TD01/code/tri-analyse.py | 261 ++++++++++++++++++++ TD01/code/tri-comparaison.py | 1 + TD01/code/tri-selection.py | 16 ++ TD01/code/tri-stabilite.py | 17 ++ TD01/code/{format.py => util-format.py} | 0 24 files changed, 612 insertions(+), 5 deletions(-) create mode 100644 TD01/code/arbre_k_aire.py create mode 100644 TD01/code/bfs-tree.py create mode 100644 TD01/code/bfs.py create mode 100644 TD01/code/dfs-arbre.py create mode 100644 TD01/code/graph-cycle.py create mode 100644 TD01/code/graph-get.py create mode 100644 TD01/code/graph-parcours-bellman.py create mode 100644 TD01/code/graph-parcours-dijkstra.py create mode 100644 TD01/code/graph-similarite.py create mode 100644 TD01/code/listes-liees-boucle.py delete mode 100644 TD01/code/listes.py rename TD01/code/{addition.py => math-addition.py} (100%) rename TD01/code/{division.py => math-division.py} (100%) rename TD01/code/{moyenne.py => math-moyenne.py} (100%) create mode 100644 TD01/code/math-multiplication-rec.py rename TD01/code/{parite.py => math-parite.py} (100%) rename TD01/code/{premier.py => math-premier.py} (100%) rename TD01/code/{racines.py => math-racines.py} (100%) create mode 100644 TD01/code/recherche2.py create mode 100644 TD01/code/tri-analyse.py create mode 100644 TD01/code/tri-comparaison.py create mode 100644 TD01/code/tri-selection.py create mode 100644 TD01/code/tri-stabilite.py rename TD01/code/{format.py => util-format.py} (100%) diff --git a/TD01/code/arbre_k_aire.py b/TD01/code/arbre_k_aire.py new file mode 100644 index 0000000..d230728 --- /dev/null +++ b/TD01/code/arbre_k_aire.py @@ -0,0 +1,43 @@ +class Noeud: + def __init__(self, v, c = []): + self.v = v + self.c = c + +def parcours_affiche(arbre): + for c in arbre.c: + parcours_affiche(c) + + print(arbre.v) + + +def parcours_recherche(arbre, valeur): + stack = arbre.c + + if valeur > arbre.v: + return False + + if valeur == arbre.v: + return True + + prev = stack[0].v + + while len(stack) > 0: + + children = stack.pop(0) + + if valeur == children.v: + return True + + if valeur > prev and valeur < children.v: + stack = children.c + prev = stack[0].v + else: + prev = children.v + + return False + + +racine = Noeud(13, [Noeud(4, [Noeud(1), Noeud(2), Noeud(3)]), Noeud(8, [Noeud(5), Noeud(6), Noeud(7)]), Noeud(12, [Noeud(9), Noeud(10), Noeud(11)])]) + +print(parcours_affiche(racine)) +print(parcours_recherche(racine, 11)) \ No newline at end of file diff --git a/TD01/code/bfs-tree.py b/TD01/code/bfs-tree.py new file mode 100644 index 0000000..8599a5d --- /dev/null +++ b/TD01/code/bfs-tree.py @@ -0,0 +1,29 @@ +import collections +class graph: + def __init__(self,gdict=None): + if gdict is None: + gdict = {} + self.gdict = gdict + +def bfs(graph, startnode): + seen, queue = set([startnode]), collections.deque([startnode]) + while queue: + vertex = queue.popleft() + marked(vertex) + for node in graph[vertex]: + if node not in seen: + seen.add(node) + queue.append(node) + +def marked(n): + print(n) + +tree = { "a" : set(["b","c"]), + "b" : set(["e", "d"]), + "c" : set(["f"]), + "f" : set(["i"]), + "e" : set(["g", "h"]), + "d" : set(), "i" : set(), "g" : set(), "h" : set() + } + +bfs(tree, "a") \ No newline at end of file diff --git a/TD01/code/bfs.py b/TD01/code/bfs.py new file mode 100644 index 0000000..3fa8624 --- /dev/null +++ b/TD01/code/bfs.py @@ -0,0 +1,31 @@ +import collections +class graph: + def __init__(self,gdict=None): + if gdict is None: + gdict = {} + self.gdict = gdict + +def bfs(graph, startnode): +# Track the visited and unvisited nodes using queue + seen, queue = set([startnode]), collections.deque([startnode]) + while queue: + vertex = queue.popleft() + marked(vertex) + for node in graph[vertex]: + if node not in seen: + seen.add(node) + queue.append(node) + +def marked(n): + print(n) + +if __name__=="__main__": + + gdict = { "a" : set(["b","c"]), + "b" : set(["a", "d"]), + "c" : set(["a", "d"]), + "d" : set(["e"]), + "e" : set(["a"]) + } + + bfs(gdict, "a") \ No newline at end of file diff --git a/TD01/code/dfs-arbre.py b/TD01/code/dfs-arbre.py new file mode 100644 index 0000000..a95a909 --- /dev/null +++ b/TD01/code/dfs-arbre.py @@ -0,0 +1,17 @@ +def dfs(graph, start): + visited, stack = set(), [start] + while stack: + vertex = stack.pop() + if vertex not in visited: + visited.add(vertex) + stack.extend(graph[vertex] - visited) + return visited + +graph = {'A': set(['B', 'C']), + 'B': set(['A', 'D', 'E']), + 'C': set(['A', 'F']), + 'D': set(['B']), + 'E': set(['B', 'F']), + 'F': set(['C', 'E'])} + +dfs(graph, 'A') # {'E', 'D', 'F', 'A', 'C', 'B'} \ No newline at end of file diff --git a/TD01/code/graph-cycle.py b/TD01/code/graph-cycle.py new file mode 100644 index 0000000..a8ba0aa --- /dev/null +++ b/TD01/code/graph-cycle.py @@ -0,0 +1,26 @@ +graph = { 0 : [1], 1 : [2], 2 : [3], 3 : [4], 4 : [1] } +# https://algocoding.wordpress.com/2015/04/02/detecting-cycles-in-a-directed-graph-with-dfs-python/ +def cycle_existe(G): + color = { u : "white" for u in G } # Noeuds tous blancs + trouve_cycle = [False] + + for u in G: # On visite tous les noeuds + if color[u] == "white": + dfs_visite(G, u, color, trouve_cycle) + if trouve_cycle[0]: + break + return trouve_cycle[0] + +def dfs_visite(G, u, color, trouve_cycle): + if trouve_cycle[0]: + return + color[u] = "gray" + for v in G[u]: + if color[v] == "gray": + trouve_cycle[0] = True + return + if color[v] == "white": + dfs_visite(G, v, color, trouve_cycle) + color[u] = "black" + +print(cycle_existe(graph)) \ No newline at end of file diff --git a/TD01/code/graph-get.py b/TD01/code/graph-get.py new file mode 100644 index 0000000..dc66259 --- /dev/null +++ b/TD01/code/graph-get.py @@ -0,0 +1,25 @@ +graph = { "a" : ["c"], + "b" : ["c", "e"], + "c" : ["a", "b", "d", "e"], + "d" : ["c"], + "e" : ["c", "b"], + "f" : [] +} + + +def genere_arretes(graph): + edges = [] + for node in graph: + for neighbour in graph[node]: + edges.append((node, neighbour)) + + return edges + +# Affiche les sommets +print(list(graph.keys())) + +# Affiche les arrêtes +print(genere_aretes(graph)) + +# >>> [('a', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'd'), ('c', 'e'), +# ('b', 'c'), ('b', 'e'), ('e', 'c'), ('e', 'b'), ('d', 'c')] \ No newline at end of file diff --git a/TD01/code/graph-parcours-bellman.py b/TD01/code/graph-parcours-bellman.py new file mode 100644 index 0000000..9df206b --- /dev/null +++ b/TD01/code/graph-parcours-bellman.py @@ -0,0 +1,20 @@ +def BellmanFord(self, src): + + # Distances infinies + dist = [float("Inf")] * self.V + dist[src] = 0 + + # Relache les sommets - 1 + for i in range(self.V - 1): + + # Met a jour noeud et parents + for u, v, w in self.graph: + if dist[u] != float("Inf") and dist[u] + w < dist[v]: + dist[v] = dist[u] + w + + # Verifie si cycle + for u, v, w in self.graph: + if dist[u] != float("Inf") and dist[u] + w < dist[v]: + print "Le graphe contient des cycles négatifs" + return + \ No newline at end of file diff --git a/TD01/code/graph-parcours-dijkstra.py b/TD01/code/graph-parcours-dijkstra.py new file mode 100644 index 0000000..2d413ba --- /dev/null +++ b/TD01/code/graph-parcours-dijkstra.py @@ -0,0 +1,43 @@ +class Graph: + def __init__(self): + self.nodes = set() + self.edges = defaultdict(list) + self.distances = {} + + def add_node(self, value): + self.nodes.add(value) + + def add_edge(self, from_node, to_node, distance): + self.edges[from_node].append(to_node) + self.edges[to_node].append(from_node) + self.distances[(from_node, to_node)] = distance + + +def dijsktra(graph, initial): + visited = {initial: 0} + path = {} + + nodes = set(graph.nodes) + + while nodes: + min_node = None + for node in nodes: + if node in visited: + if min_node is None: + min_node = node + elif visited[node] < visited[min_node]: + min_node = node + + if min_node is None: + break + + nodes.remove(min_node) + current_weight = visited[min_node] + + for edge in graph.edges[min_node]: + weight = current_weight + graph.distance[(min_node, edge)] + if edge not in visited or weight < visited[edge]: + visited[edge] = weight + path[edge] = min_node + + return visited, path \ No newline at end of file diff --git a/TD01/code/graph-similarite.py b/TD01/code/graph-similarite.py new file mode 100644 index 0000000..43ee064 --- /dev/null +++ b/TD01/code/graph-similarite.py @@ -0,0 +1,37 @@ +# Par exemple pour Fool et Sage vous devez renvoyez le résultat suivant : +# +# FOOL +# POOL +# POLL +# POLE +# PALE +# SALE +# SAGE +# +# SOLUTION: + +graph = {'A': ['B', 'C'], + 'B': ['C', 'D'], + 'C': ['D'], + 'D': ['C'], + 'E': ['F'], + 'F': ['C'] +} + +def chemin(graphe, source, destination, visite=None): + + if source == destination: + return [destination] + else: + visite = visite or set() + for s in graphe[source]: + if s not in visite: + visite.add(s) + print("trace:", source + " > " + s) + sous_chemin = chemin(graphe, s, destination, visite) + if sous_chemin is not None: + return [source] + sous_chemin + +print(chemin(simGraphe(["AA", "AB", "BC", "AC", "DD"]), "AA", "BC")) + +# ['AA', 'AB', 'AC', 'BC'] \ No newline at end of file diff --git a/TD01/code/listes-liees-boucle.py b/TD01/code/listes-liees-boucle.py new file mode 100644 index 0000000..1b9fa75 --- /dev/null +++ b/TD01/code/listes-liees-boucle.py @@ -0,0 +1,15 @@ +from LinkedList import * + +ll = LinkedList() # Ajout de données +ll.push(20) +ll.push(4) +ll.push(15) +ll.push(10) + +# Création d'une boucle +ll.head.next.next.next.next = ll.head; + +if( ll.detectLoop()): + print ("Il y a une boucle !") +else : + print ("Pas de boucle ! ") \ No newline at end of file diff --git a/TD01/code/listes.py b/TD01/code/listes.py deleted file mode 100644 index 8c4eab0..0000000 --- a/TD01/code/listes.py +++ /dev/null @@ -1,5 +0,0 @@ - - -L = [] - -L.ap \ No newline at end of file diff --git a/TD01/code/addition.py b/TD01/code/math-addition.py similarity index 100% rename from TD01/code/addition.py rename to TD01/code/math-addition.py diff --git a/TD01/code/division.py b/TD01/code/math-division.py similarity index 100% rename from TD01/code/division.py rename to TD01/code/math-division.py diff --git a/TD01/code/moyenne.py b/TD01/code/math-moyenne.py similarity index 100% rename from TD01/code/moyenne.py rename to TD01/code/math-moyenne.py diff --git a/TD01/code/math-multiplication-rec.py b/TD01/code/math-multiplication-rec.py new file mode 100644 index 0000000..8485f42 --- /dev/null +++ b/TD01/code/math-multiplication-rec.py @@ -0,0 +1,8 @@ +def mult(a, b): + if(b == 1): + return a + else: + return a + mult(a, b-1) + + +print(10 * 13, mult(10, 13)) diff --git a/TD01/code/parite.py b/TD01/code/math-parite.py similarity index 100% rename from TD01/code/parite.py rename to TD01/code/math-parite.py diff --git a/TD01/code/premier.py b/TD01/code/math-premier.py similarity index 100% rename from TD01/code/premier.py rename to TD01/code/math-premier.py diff --git a/TD01/code/racines.py b/TD01/code/math-racines.py similarity index 100% rename from TD01/code/racines.py rename to TD01/code/math-racines.py diff --git a/TD01/code/recherche2.py b/TD01/code/recherche2.py new file mode 100644 index 0000000..97a92e0 --- /dev/null +++ b/TD01/code/recherche2.py @@ -0,0 +1,23 @@ +def recherche(L, v): + + if(len(L) <= 0): + return False + + a = 0 + b = len(L) + m = b // 2 + + if(L[m] == v): + return True + elif(L[m] > v): + return recherche(L[0:m], v) + elif(L[m] < v): + return recherche(L[m+1:b], v) + else: + return False + +if __name__=="__main__": + assert recherche([1, 2, 3], 1) + assert recherche([], 1) == False + assert recherche([1], 1) == True + assert recherche([1, 2, 3], 3) == True \ No newline at end of file diff --git a/TD01/code/tri-analyse.py b/TD01/code/tri-analyse.py new file mode 100644 index 0000000..fcaefc7 --- /dev/null +++ b/TD01/code/tri-analyse.py @@ -0,0 +1,261 @@ +# Comparaison des différents tris +import time +import random +import matplotlib.pyplot as plt +import numpy as np + +# On commence par définir une petite méthode "utilitaire" qui va nous servir souvent. +# permute permet de permuter deux éléments situés aux indices i et j d'un tableau. +def permute(tab, i, j): + ''' + :e/s: tab, un tableau d'éléments + :entrees i et j: (int) les indices des éléments à permuter + :pre-cond: i et j sont des indices valides dans tab + :post-cond: le tableau est modifié, deux valeurs sont perumtées + ''' + temp = tab[i] + tab[i] = tab[j] + tab[j] = temp + +# Tri par sélection +# Rappel : on recherche le minimum du tableau, et on le permute avec l'élément dans la première case. +# Ensuite, on recherche le minimum dans le reste du tableau, et on le permute avec l'élément dans la 2ème case. +# Et ainsi de suite... + + +def triSelection(tab): + ''' + :e/s tab: tableau (de float) + :post-cond: + - ∀ i ∈ [0;len(tab)[, ∃ j ∈ [0;len(tab)[, tabₛ[j] = tabₑ[i] + (les éléments de tab ont simplent changé d'ordre) + - ∀ i ∈ [1;len(tab)[, tabₛ[i] ≥ tabₛ[i-1] + (les éléments de tab sont triés par valeur croissante) + :complexité : 𝓞(n²) + ''' + for i in range(len(tab)-1): + indice_min = i + + # Boucle de recherche du minimum dans le tableau restant + for j in range(i+1,len(tab)): + if tab[j]<tab[indice_min]: + indice_min=j + + # Une fois le min trouvé, on le permute pour le mettre à sa place + permute(tab, i, indice_min) + + +# Tri par insertion +# Rappel : on considère que le "début" du tableau est trié. +# On se place au niveau de la première valeur non triée, et on la décale à gauche jusqu'à ce qu'elle trouve sa place +# Observez que la partie gauche du tableau est déjà triée... + + +# On commence par écrire une petite fonction utilitaire (une autre). +# Cette fonction prend un élément à un indice i, et le décale sur +# sa gauche jusqu'à ce qu'il soit à sa place... en faisant +# l'hypothèse que tous les éléments sur sa gauche sont bien triés. + +def insereElt(tab, i): + ''' + :e/s tab: tableau d'éléments + :entrée i: int + :pré-cond: + - 1 ≤ i < len(tab) + - ∀ j ∈ [1;i[, tab[j] ≥ tab[j-1] (tab est trié entre 0 et i-1) + :post-cond: + - ∀ j ∈ [0;i+1[, ∃ k ∈ [0;i+1[, tabₛ[k] = tabₑ[j] + (les éléments entre 0 et i+1 ont simplement changé d'ordre) + - ∀ j ∈ [i+1;len(tab)[, tabₛ[j] = tabₑ[j] + (les éléments au delà de i n'ont pas été modifiés) + - ∀ j ∈ [1;i+1[, tab[j] ≥ tab[j-1] + (tab est trié entre 0 et i) + ''' + while (tab[i-1] > tab[i]) and i>0 : + permute(tab, i, i-1) + i -= 1 + +# On écrit ensuite l'algo principal qui consiste à prendre +# tour à tour chaque élément "non trié", et à l'insérer dans +# le tableau trié (c'est-à-dire à le décaler jusqu'à sa bonne +# place sur sa gauche). +def triInsertion(tab): + ''' + :e/s tab: tableau d'éléments + :pré- et post-conditions usuelles + ''' + for i in range(1,len(tab)): + insereElt(tab, i) + + +# Tri binaire, ou quicksort +# Le quicksort est un tri récursif par excellence. +# Le principe est que l'on choisit une valeur pivot, +# on range toutes les valeurs plus petites que le pivot à gauche du pivot +# et toutes les valeurs plus grandes à droite. +# Ensuite, on applique la même démarche sur les sous-tableaux gauche et droite. +# Une simulation intéressante est disponible ici : +# http://interactivepython.org/runestone/static/pythonds/SortSearch/TheQuickSort.html + + +# On définit une méthode utilitaire qui va nous aider à partitionner notre tableau autour d'un pivot +def partitionne(tab, imin, imax): + ''' + :e/s tab: tableau d'éléments + :entrée imin: int + :entrée imax: int + :sortie idroite: int + :pré-cond: 0 ≤ imin ≤ imax < len(tab) + :post-cond: + - imin ≤ idroite ≤ imax + - ∀ i ∈ [0;imin[ U ]imax;len(tab)[, tabₛ[i] = tabₑ[i] + (tab n'est pas modifié en dehors de la plage [imin;imax]) + - ∀ i ∈ [imin;imax], ∃ j ∈ [imin;imax], tabₛ[j] = tabₑ[i] + (les éléments de tab ont simplent changé d'ordre entre imin et imax) + - ∀ i ∈ [imin;idroite], tabₛ[i] ≤ tabₛ[idroite] + (les éléments à gauche du pivot lui sont inférieurs ou égaux) + - ∀ i ∈ ]idroite;imax], tabₛ[i] > tabₛ[idroite] + (les éléments à droite du pivot lui sont supérieurs) + ''' + pivot = tab[imin] # On choisit arbitrairement le premier élément comme pivot + + igauche = imin + 1 + idroite = imax + fini = False + while not fini: + while igauche <= idroite and tab[igauche] <= pivot: + igauche = igauche + 1 + while tab[idroite] >= pivot and idroite >= igauche: + idroite = idroite - 1 + if idroite < igauche: + fini= True + else: + temp = tab[igauche] + tab[igauche] = tab[idroite] + tab[idroite] = temp + #permute(tab, igauche, idroite) + temp = tab[imin] + tab[imin] = tab[idroite] + tab[idroite] = temp + #permute(tab, imin, imax) + return idroite + + +# Si notre tableau n'est pas vide, on appelle tri_rec : +# Sinon, le tableau vide n'est pas modifié. +def triRecursif(tab): + if len(tab) > 0: + tri_rec(tab, 0, len(tab)-1) + +def tri_rec(tab, imin, imax): + ''' + :e/s tab: tableau d'éléments + :entrée imin: int + :entrée imax: int + :pré-cond: 0 ≤ imin ≤ imax < len(tab) (les indices existent dans le tableau) + :post-cond: + - ∀ i ∈ [0;imin[ U ]imax;len(tab)[, tabₛ[i] = tabₑ[i] + (tab n'est pas modifié en dehors de la plage [imin;imax]) + - ∀ i ∈ [imin;imax], ∃ j ∈ [imin;imax], tabₛ[j] = tabₑ[i] + (les éléments de tab ont simplent changé d'ordre entre imin et imax) + - ∀ i ∈ [imin;imax[, tabₛ[i+1] ≥ tabₛ[i] + (tab est trié entre imin et imax) + ''' + if imin < imax: + # partition the list + pivot = partitionne(tab, imin, imax) + # sort both halves + tri_rec(tab, imin, pivot-1) + tri_rec(tab, pivot+1, imax) + return tab + +# Tri bulle +def triBulle(tab): + ''' + :entree/sortie: tab un tableau d'éléments + :post-condition : tab est trié + ''' + swap = True + while swap == True: + swap = False + for j in range(0,len(tab)-1): + if tab[j]>tab[j+1]: + swap = True + temp=tab[j] + tab[j]=tab[j+1] + tab[j+1]=temp + + + + +nvalues = [10, 100, 500, 1000] +timesSelection = [] +timesInsertion = [] +timesBulle = [] +timesRecursif = [] + +for i in nvalues: + random.seed() + p = 12**2 # Ordre de grandeur des valeurs + liste = [] + + for x in range(i): liste.append(random.randint(0, p)) + + tableau = list(liste) + a=time.process_time() + triSelection(tableau) + if x <= 10: + print(tableau) + b=time.process_time() + timesSelection.append(b-a) + + tableau = list(liste) + a=time.process_time() + triInsertion(tableau) + if x <= 10: + print(tableau) + b=time.process_time() + timesInsertion.append(b-a) + + tableau = list(liste) + a=time.process_time() + triRecursif(tableau) + if x <= 10: + print(tableau) + b=time.process_time() + timesRecursif.append(b-a) + + tableau = list(liste) + a=time.process_time() + triBulle(tableau) + if x <= 10: + print(tableau) + b=time.process_time() + timesBulle.append(b-a) + +print(nvalues) + +print(timesSelection) +print(timesInsertion) +print(timesRecursif) +print(timesBulle) + + +#xs = range(0,1000) +plt.plot(nvalues,timesSelection, "r-", label="Tri par sélection") +plt.plot(nvalues,timesInsertion, "g-", label="Tri par insertion") +plt.plot(nvalues,timesRecursif, "b-", label="Tri par récursif") +plt.plot(nvalues,timesBulle, "g--", label="Tri bulles") + +#plt.plot(nvalues,timesRecursif, "b-", label="Quick sort") +#plt.plot(xs, sqrt(xs), "r-", label=" √n") +plt.title("Comparaison des performances des différents algorithmes de tri") + + + +plt.savefig('analyse-tri.png') + +# Outil pour tester les algos de tri à la main +tableauATrier = [42,1,6,0,8,9,2,4,7,3,19,34,23,67,45,23,105,18,190,20] +triBulle(tableauATrier) +print(tableauATrier) diff --git a/TD01/code/tri-comparaison.py b/TD01/code/tri-comparaison.py new file mode 100644 index 0000000..07a5499 --- /dev/null +++ b/TD01/code/tri-comparaison.py @@ -0,0 +1 @@ +# TODO: comparaison de différents tris \ No newline at end of file diff --git a/TD01/code/tri-selection.py b/TD01/code/tri-selection.py new file mode 100644 index 0000000..8b96495 --- /dev/null +++ b/TD01/code/tri-selection.py @@ -0,0 +1,16 @@ +def selectionSort(alist): + + for l in range(len(alist )-1,0,-1): + positionOfMax =0 + + for location in range (1, l+1): + if alist[location]>alist[ positionOfMax ]: + positionOfMax = location + + temp = alist[l] + alist[l] = alist[positionOfMax] + alist[positionOfMax] = temp + +alist = [54 ,26 ,93 ,17 ,77 ,31 ,44 ,55 ,20] +selectionSort(alist) +print(alist) \ No newline at end of file diff --git a/TD01/code/tri-stabilite.py b/TD01/code/tri-stabilite.py new file mode 100644 index 0000000..3e4e78d --- /dev/null +++ b/TD01/code/tri-stabilite.py @@ -0,0 +1,17 @@ +def algo(words): + t = [] + for word in words: + t.append((len(word), word)) + + t.sort(key=lambda t: t[1][-1]) + + res = [] + for l, w in t: + res.append(w) + + res = filter(lambda t: 'a' in t, res) + + return list(res) + + +print(algo(["Pierre", "Jean", "Marie", "Eric", "Nathalie", "Yvonne"])) \ No newline at end of file diff --git a/TD01/code/format.py b/TD01/code/util-format.py similarity index 100% rename from TD01/code/format.py rename to TD01/code/util-format.py -- GitLab