1 | initial version |
I propose to improve @Frédéric's solution as below to avoid errors in certain cases :
tuplesL=[ ((1,5,8,4),(2,6,7,3)), ((4,5)),((1,2,3)),((1,2,3),(4,5)),((1,2,3),(4,5),(7,8))]
for t in tuplesL :
print('permutation tuples= ',t)
try :
test=Permutation(t)
print('FredericC solution OK : ',[L[test(i + 1) - 1] for i in range(len(L))])
except :
print('error from FredericC solution')
P=Permutation(t)
Pletters= [L[P(i + 1) - 1] for i in range(len(P.dict()))] +[L[i] for i in range(len(P.dict()),len(L))]
print(Pletters)
But unfortunately this solution does not allow singletons in list of tuples ! like Wolfram alpha do
2 | No.2 Revision |
I propose to improve @Frédéric's @FrédéricC's solution as below to avoid errors in certain cases :
tuplesL=[ ((1,5,8,4),(2,6,7,3)), ((4,5)),((1,2,3)),((1,2,3),(4,5)),((1,2,3),(4,5),(7,8))]
for t in tuplesL :
print('permutation tuples= ',t)
try :
test=Permutation(t)
print('FredericC solution OK : ',[L[test(i + 1) - 1] for i in range(len(L))])
except :
print('error from FredericC solution')
P=Permutation(t)
Pletters= [L[P(i + 1) - 1] for i in range(len(P.dict()))] +[L[i] for i in range(len(P.dict()),len(L))]
print(Pletters)
But unfortunately this solution does not allow singletons in list of tuples ! like Wolfram alpha do
3 | No.3 Revision |
I propose to improve @FrédéricC's solution as below to avoid errors in certain cases :
tuplesL=[ ((1,5,8,4),(2,6,7,3)), ((4,5)),((1,2,3)),((1,2,3),(4,5)),((1,2,3),(4,5),(7,8))]
for t in tuplesL :
print('permutation tuples= ',t)
try :
test=Permutation(t)
print('FredericC solution OK : ',[L[test(i + 1) - 1] for i in range(len(L))])
except :
print('error from FredericC solution')
P=Permutation(t)
Pletters= [L[P(i + 1) - 1] for i in range(len(P.dict()))] +[L[i] for i in range(len(P.dict()),len(L))]
print(Pletters)
But unfortunately this solution does not allow singletons in list of tuples ! like Wolfram alpha do
see Max Alekseyev solution but unfortunately it does not accept element which appears more than once in the input like Wolfram does
4 | No.4 Revision |
I propose to improve @FrédéricC's solution as below to avoid errors in certain cases :
tuplesL=[ ((1,5,8,4),(2,6,7,3)), ((4,5)),((1,2,3)),((1,2,3),(4,5)),((1,2,3),(4,5),(7,8))]
for t in tuplesL :
print('permutation tuples= ',t)
try :
test=Permutation(t)
print('FredericC solution OK : ',[L[test(i + 1) - 1] for i in range(len(L))])
except :
print('error from FredericC solution')
P=Permutation(t)
Pletters= [L[P(i + 1) - 1] for i in range(len(P.dict()))] +[L[i] for i in range(len(P.dict()),len(L))]
print(Pletters)
But unfortunately this solution does not allow singletons in list of tuples ! like Wolfram alpha do
see Max Alekseyev solution but unfortunately it does not accept element which appears more than once in the input like Wolfram does
As this post has become a bit messy, final correct answer from Max Alekseyev to the question below:
import string
import sage.combinat.permutation as permutation
nl=12 # letters number
L=list(string.ascii_lowercase[0:nl])
tuplesL=[ ((1,5,8,4),(2,6,7,3)), ((4,5),),((1,2,3),),((1,2,3),(4,5),),((1,2,3),(4,5),(7,8)),((1, 2 ,6),(3, 5),),
((1, 2, 3, 4),(1, 3)(2, 4),)]
for t in tuplesL :
print('permutation tuples= ',t)
try :
#test=permutation.from_cycles(len(L),t)
test = prod(map(lambda x: permutation.from_cycles(len(L),[x]),t))
print('Max Alekseyev solution OK : ',[L[test[i] - 1] for i in range(len(L))])
except :
print('error ')