1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| # -*- coding: cp1252 -*-
# une méthode itérative pour générer le groupe symétrique
#cycle opérant sur les p derniers éléments
def cycle (n,p):
L1=range(0,n-p)
L2=[i+1 for i in range(n-p,n-1)]
return L1+L2+[n-p]
# composée de 2 permutations
def compose (P1,P2):
return [P2[P1[i]] for i in range(0,len(P1))]
# puissance d'une permutation
def puissance (P,n):
PUISS=range(0,len(P))
for i in range(0,n):
PUISS=compose(PUISS,P)
return PUISS
# générer toutes les permutations
def permutations (n):
CYCLES=[cycle(n,2+i) for i in range(0,n-1)]
PERM1=[cycle(n-1,0)]
for i in range (0,n-1):
PERM2=[compose(PERM1[k],puissance(CYCLES[i],j)) for j in range(0,i+2) for k in range(0,len(PERM1))]
PERM1=PERM2
return PERM2
#programme principal
print permutations(5) |
Partager