Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal Permutations Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal Fonction auxiliaire no 1 La fonction add1 La fonction suivante prend pour argument une liste et renvoie la liste dont tous les éléments sont augmentés de 1. Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal Fonction auxiliaire no 1 La fonction add1 La fonction suivante prend pour argument une liste et renvoie la liste dont tous les éléments sont augmentés de 1. def add1 ( l ) : res =[] for x in l : res . append ( x +1) return res Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Fonction auxiliaire no 1 La fonction add1 La fonction suivante prend pour argument une liste et renvoie la liste dont tous les éléments sont augmentés de 1. def add1 ( l ) : res =[] for x in l : res . append ( x +1) return res Exécution Programme principal >>> l =[3 ,5 ,6] >>> add1 ( l ) [4 , 6 , 7] Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal Fonction auxiliaire no 2 La fonction teste La fonction suivante prend pour argument une liste de longueur n et teste si elle contient tous les éléments de J0, n − 1K. Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal Fonction auxiliaire no 2 La fonction teste La fonction suivante prend pour argument une liste de longueur n et teste si elle contient tous les éléments de J0, n − 1K. def teste ( l ) : n = len ( l ) for i in range ( n ) : if not ( i in l ) : return False return True Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonction auxiliaire no 2 La fonction teste La fonction suivante prend pour argument une liste de longueur n et teste si elle contient tous les éléments de J0, n − 1K. def teste ( l ) : n = len ( l ) for i in range ( n ) : if not ( i in l ) : return False return True Fonctions auxiliaires Programme principal Exécution >>> teste ([1 ,2 ,4 ,0 ,3]) >>> True Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal Fonction auxiliaire no 3 La fonction convertbase10 La fonction suivante prend pour arguments un entier b et une liste l et rend le nombre dont l est la représentation en base b. Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal Fonction auxiliaire no 3 La fonction convertbase10 La fonction suivante prend pour arguments un entier b et une liste l et rend le nombre dont l est la représentation en base b. def convertbase10 (b , l ) : m = len ( l ) x =1 res =0 for i in range (m ,0 , -1) : res = res + l [i -1]* x x=x*b return ( res ) Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal Fonction auxiliaire no 3 La fonction convertbase10 La fonction suivante prend pour arguments un entier b et une liste l et rend le nombre dont l est la représentation en base b. def convertbase10 (b , l ) : m = len ( l ) x =1 res =0 for i in range (m ,0 , -1) : res = res + l [i -1]* x x=x*b return ( res ) Exécution >>> l =[1 ,2 ,4 ,0 ,3] >>> convertbase10 (5 , l ) 978 Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal Fonction auxiliaire no 4 La fonction convert10base La fonction suivante prend pour arguments deux entierx b et x et rend sous forme de liste l’écriture de x en base b. Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal Fonction auxiliaire no 4 La fonction convert10base La fonction suivante prend pour arguments deux entierx b et x et rend sous forme de liste l’écriture de x en base b. def convert10base (b , x ) : l =[] a=x while a != 0: l . append ( a % b ) a = a // b l . reverse () return ( l ) Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal Fonction auxiliaire no 4 La fonction convert10base La fonction suivante prend pour arguments deux entierx b et x et rend sous forme de liste l’écriture de x en base b. def convert10base (b , x ) : l =[] a=x while a != 0: l . append ( a % b ) a = a // b l . reverse () return ( l ) Exécution >>> convert10base (5 ,978) [1 , 2 , 4 , 0 , 3] Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal Programme principal L’algorithme La fonction suivante prend pour argument un entier n et rend la liste des permutations (éléments du groupe symétrique Sn ). 1 Soient l1 = [0, 1, 2, ..., n − 1] et l2 = [n − 1, n − 2, ..., 1, 0] représentations en base n des deux nombres a et b Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction suivante prend pour argument un entier n et rend la liste des permutations (éléments du groupe symétrique Sn ). 1 Soient l1 = [0, 1, 2, ..., n − 1] et l2 = [n − 1, n − 2, ..., 1, 0] représentations en base n des deux nombres a et b 2 On parcourt l’intervalle d’entiers Ja, b K Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction suivante prend pour argument un entier n et rend la liste des permutations (éléments du groupe symétrique Sn ). 1 Soient l1 = [0, 1, 2, ..., n − 1] et l2 = [n − 1, n − 2, ..., 1, 0] représentations en base n des deux nombres a et b 2 On parcourt l’intervalle d’entiers Ja, b K Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal 3 Pour chaque élément de cet intervalle, on le représente en base n. Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction suivante prend pour argument un entier n et rend la liste des permutations (éléments du groupe symétrique Sn ). 1 Soient l1 = [0, 1, 2, ..., n − 1] et l2 = [n − 1, n − 2, ..., 1, 0] représentations en base n des deux nombres a et b 2 On parcourt l’intervalle d’entiers Ja, b K Programme principal L’algorithme La fonction L’exécution 3 Pour chaque élément de cet intervalle, on le représente en base n. 4 ... et on vérifie si la liste l obtenue contient tous les éléments de J0, n − 1K Et la récursivité ? Fonctions auxiliaires Programme principal Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction suivante prend pour argument un entier n et rend la liste des permutations (éléments du groupe symétrique Sn ). 1 Soient l1 = [0, 1, 2, ..., n − 1] et l2 = [n − 1, n − 2, ..., 1, 0] représentations en base n des deux nombres a et b 2 On parcourt l’intervalle d’entiers Ja, b K Programme principal L’algorithme La fonction L’exécution 3 Pour chaque élément de cet intervalle, on le représente en base n. 4 ... et on vérifie si la liste l obtenue contient tous les éléments de J0, n − 1K Et la récursivité ? Fonctions auxiliaires Programme principal 5 Si oui, on ajoute 1 à chaque élément de l et on ajoute la liste obtenue dans la liste résultat. Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction suivante prend pour argument un entier n et rend la liste des permutations (éléments du groupe symétrique Sn ). 1 Soient l1 = [0, 1, 2, ..., n − 1] et l2 = [n − 1, n − 2, ..., 1, 0] représentations en base n des deux nombres a et b 2 On parcourt l’intervalle d’entiers Ja, b K Programme principal L’algorithme La fonction L’exécution 3 Pour chaque élément de cet intervalle, on le représente en base n. 4 ... et on vérifie si la liste l obtenue contient tous les éléments de J0, n − 1K Et la récursivité ? Fonctions auxiliaires Programme principal 5 Si oui, on ajoute 1 à chaque élément de l et on ajoute la liste obtenue dans la liste résultat. Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal Programme principal La fonction def permutations ( n ) : res =[ add1 ( l ) ] a = convertbase10 (n , l ) l1 = list ( l ) # copie de la liste l l1 . reverse () b = convertbase10 (n , l1 ) for k in range ( a +1 ,b -1) : l = convert10base (n , k ) if len ( l ) <n : # cas particulier l =[0]+ l if teste ( l ) : res . append ( add1 ( l ) ) res . append ( add1 ( l1 ) ) return ( res , len ( res ) ) Permutations Fonctions auxiliaires La fonction add1 Programme principal L’exécution Exécution La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal >>> permutations (3) ([[1 ,2 ,3] ,[1 ,3 ,2] ,[2 ,1 ,3] , [2 ,3 ,1] ,[3 ,1 ,2] ,[3 ,2 ,1]] , 6) >>> permutations (4) [[1 ,2 ,3 ,4] ,[1 ,2 ,4 ,3] ,[1 ,3 ,2 ,4] ,[1 ,3 ,4 ,2] , [1 ,4 ,2 ,3] ,[1 ,4 ,3 ,2] ,[2 ,1 ,3 ,4] ,[2 ,1 ,4 ,3] , [2 ,3 ,1 ,4] ,[2 ,3 ,4 ,1] ,[2 ,4 ,1 ,3] ,[2 ,4 ,3 ,1] , [3 ,1 ,2 ,4] ,[3 ,1 ,4 ,2] ,[3 ,2 ,1 ,4] ,[3 ,2 ,4 ,1] , [3 ,4 ,1 ,2] ,[3 ,4 ,2 ,1] ,[4 ,1 ,2 ,3] ,[4 ,1 ,3 ,2] , [4 ,2 ,1 ,3] ,[4 ,2 ,3 ,1] ,[4 ,3 ,1 ,2] ,[4 ,3 ,2 ,1]] , 24) Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal Solution récursive Les deux fonctions auxiliaires On utilise deux fonctions auxiliaires dont les effets sont décrits ci-dessous : 1 La fonction ajouter In [34]: ajouter (5 ,[2 ,1 ,3 ,4] ,3) Out [34]: [2 , 1 , 3 , 5 , 4] Permutations Fonctions auxiliaires La fonction add1 La fonction teste La fonction convertbase10 Solution récursive Les deux fonctions auxiliaires On utilise deux fonctions auxiliaires dont les effets sont décrits ci-dessous : 1 La fonction convert10base La fonction ajouter In [34]: ajouter (5 ,[2 ,1 ,3 ,4] ,3) Out [34]: [2 , 1 , 3 , 5 , 4] Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal 2 La fonction générer In [48]: generer ([1 ,2 ,3 ,4]) Out [48]: [[1 , 2 , 3 , 4 , 5] , [1 , 2 , 3 , 5 , 4] , [1 , 2 , 5 , 3 , 4] , [1 , 5 , 2 , 3 , 4] , [5 , 1 , 2 , 3 , 4]] Permutations Fonctions auxiliaires La fonction add1 Solution récursive Le programme principal La fonction teste La fonction convertbase10 La fonction convert10base Programme principal L’algorithme La fonction L’exécution Et la récursivité ? Fonctions auxiliaires Programme principal def permutations_rec ( n ) : if n ==1: # cas de base return [[1]] else : # appel r é cursif l = permutations_rec (n -1) res =[] for p in l : res . extend ( generer ( p ) ) return res