Processing math: 100%

First time here? Check out the FAQ!

Ask Your Question
0

apply Permutation cycles to a letters list

asked 0 years ago

ortollj gravatar image

updated 0 years ago

Hi

Sorry for this very basic question, but how to apply the Permutation function to a list of letters ? I think it should exist a simplest way than the the cooking below. I searched the web, without success !

def applyGen(ge,dic) :   
    return [ge[dic.get(e)] for e in dic.keys() ]

def decrement_dic(dic): 
    return {k - 1: v - 1 for k, v in dic.items()} 

lettersL=['a','b','c','d','e','f','g','h']

test=Permutation((1,5,8,4),(2,6,7,3))
dicD=decrement_dic(test.dict())
applyGen(lettersL,dicD)
Preview: (hide)

3 Answers

Sort by » oldest newest most voted
2

answered 0 years ago

FrédéricC gravatar image

updated 0 years ago

Like this

sage: L = ['a','b','c','d','e','f','g','h']
sage: test = Permutation((1,5,8,4),(2,6,7,3))
sage: [L[test(i + 1) - 1] for i in range(len(L))]
['e', 'b', 'c', 'a', 'h', 'f', 'g', 'd']

Note that Python numbering starts from 0.

Preview: (hide)
link

Comments

ok thank you @FrédéricC

ortollj gravatar imageortollj ( 0 years ago )

Btw, one can replace test(i + 1) with test[i].

Max Alekseyev gravatar imageMax Alekseyev ( 0 years ago )

@Max Alekseyev What do you mean ?

ortollj gravatar imageortollj ( 0 years ago )
1

I mean that [L[test[i] - 1] for i in range(len(L))] will work equally well.

Max Alekseyev gravatar imageMax Alekseyev ( 0 years ago )
ortollj gravatar imageortollj ( 0 years ago )
1

answered 0 years ago

dan_fulea gravatar image

Here is an alternative solution that explicitly uses the set X=1,2,,8 where the permutation is acting on, and a dictionary dic that passes from X to the letters. The dictionary is a replacement for the increment/decrement that converts between pythonic indexing and the natural set permutations act on...

letters = list('abcdefgh')
X = list(range(1, len(letters) + 1))
dic = dict( zip(X, letters) )

test_pi = Permutation((1,5,8,4), (2,6,7,3))
[dic[test_pi(x)] for x in X]

We obtain:

['e', 'b', 'c', 'a', 'h', 'f', 'g', 'd']
sage: ''.join(_)
'ebcahfgd'
Preview: (hide)
link
0

answered 0 years ago

ortollj gravatar image

updated 0 years ago

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 ')
Preview: (hide)
link

Comments

But that doesn't accept singletons is a minor problem

ortollj gravatar imageortollj ( 0 years ago )
1

First, to specify tuples with single cycles, you need to have an extra "," - like ((4,5),) and ((1,2,3),). Second, to construct permutations with implicut presence of singletons you can replace test=Permutation(t) with

import sage.combinat.permutation as permutation
test = permutation.from_cycles(len(L),t)
Max Alekseyev gravatar imageMax Alekseyev ( 0 years ago )

Thank you @Max Alekseyev

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),)]
for t in tuplesL :
    print('permutation tuples= ',t)
    try :
        test=permutation.from_cycles(len(L),t)
        print('Max Alekseyev solution OK : ',[L[test[i] - 1] for i in range(len(L))])
    except :
        print('error from Max Alekseyev solution solution')

is it possible to transform Max Alekseyev solution as THE solution of this question ?

ortollj gravatar imageortollj ( 0 years ago )

Just edit your own answer.

Max Alekseyev gravatar imageMax Alekseyev ( 0 years ago )

I wanted to copy Max Alekseyev's solution into the incomplete solution I gave.So first, I tried to transform my answer into a comment using the menu --> more --> repost as a question comment, but nothing happens.

ortollj gravatar imageortollj ( 0 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 0 years ago

Seen: 246 times

Last updated: Dec 27 '24