How to implement a Python permutation algorithm?

Asked

Viewed 1,748 times

1

I’m trying to implement a Python permutation algorithm.

He receives as input a phrase and a key (numerical value) which corresponds to the number of letters that each group should have, and finally, a numerical sequence from 1 to the key value.

Follow an example of data entry:

Catchphrase:

"Python is the best language!!"

Passkey:

5

Input sequence:

4 2 5 1 3

Expected exit:

Hyopt
méena
rhllo
unaig
!e! gm


Follow my attempt, but without result, because I am not able to implement and do the division by groups to get the result illustrated in the example.

Code:

contador = 0            
frase = input('Digite uma frase: ').upper
chave = int(input('Digite a chave: '))
seq = []

while contador <= chave:    
  seq.append(int(input('Digite o número da sequência: ')))

frase = frase.split()
frase = ''.join(frase)
  • What exactly is the logic to generate the expected output?

  • 1º Add the sentence in a word only 2 Divide into groups of letters 3º Change the order of each letter within each group, according to the sequence given

  • And what is the criterion for defining the groups of letters in step 2? Have the number of characters defined by the key?

  • The criterion is the value of the informed key, which corresponds to the number of letters in each group

  • And if the text length is not multiple of the key, will the last group have fewer characters? If the key is 5 and the text has 12 characters, it will create groups of 5, 5 and 2 characters?

  • The last group must be completed with an asterisk *

  • Okay, once you’ve defined the groups, what’s the logic for permuting the characters based on the input sequence? Only scroll characters within the group itself?

  • The sequence numbers correspond to the position of each character. For example: In the character group "cafe", with sequence "3 1 4 2" the group will have the string in the following order: "fcea". It means that the character of position 3 will take position 1, the character of position 1 will take position 2, and so on

  • Now it is clear. I reply as soon as possible.

Show 4 more comments

1 answer

1


The separation of text into groups can be done through a list understanding by accessing the positions i:i+key of the text, where i varies from 0 to the length of the text, with step equal to key. When the last group does not have the required amount of characters, it is added * using the method ljust. After the groups are traversed and the permutation between the characters.

# Frase de entrada:
text = input("Entre com uma frase: ")

# Chave de entrada:
key = int(input("Entre com uma chave: "))

# Sequência de entrada:
sequence = []
for i in range(key):
    sequence.append(int(input("Entre com um número da sequência: ")))

# Remove os espaços em branco do texto:
text = text.replace(" ", "")

# Divide o texto em grupos definidos pela chave:
groups = [text[i:i+key].ljust(key, "*") for i in range(0, len(text), key)]

# Percorre os grupos gerando os novos grupos:
result = []
for group in groups:
    output = ""
    for i in sequence:
        output += group[i-1]
    result.append(output)

print(result)

With the entries presented in the question, the output is:

['hyoPt', 'méena', 'rhllo', 'unaig', '!e!gm']

See working on Ideone.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.