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?
– Woss
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
– Lucas Moraes
And what is the criterion for defining the groups of letters in step 2? Have the number of characters defined by the key?
– Woss
The criterion is the value of the informed key, which corresponds to the number of letters in each group
– Lucas Moraes
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?
– Woss
The last group must be completed with an asterisk *
– Lucas Moraes
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?
– Woss
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
– Lucas Moraes
Now it is clear. I reply as soon as possible.
– Woss