I want to change a string in 3 positions

Asked

Viewed 1,309 times

5

I would like to walk 3 positions with a letter of a string, for example, make the letter A turn D, I tried the second command:

texto[c] = texto[c] + 3

But it still doesn’t work, what would be the right way to do?

3 answers

6

If I get it, you want to do something similar to Cesar’s cipher. Given a string, you need to walk with each character plus 3 positions.

You can use a base alphabet, in python it is already implemented by default in string

from string import ascii_lowercase as alfabeto

so you can have a base to rotate. Beauty, we have a string base. So let’s take a string to put in the game:

string = 'stack'

String has a method called .index which will give you the position of each character. For example:

alfabeto.index('a')
# 0

so you can add 3 more and get a new position. In the same case:

alfabeto[alfabeto.index('a') + 3]
# 'd'

In this case, you can iterate on the string you want to rotate

for letra in 'stack':
    print(alfabeto[alfabeto.index(letra) + 3)
# 'v'
# 'w'
# 'd'
# 'f'
# 'n'

This way you would have a fully rotated string. However, there are some problems, as if you try to rotate 'z'

for letra in 'zzz':
    print(alfabeto[alfabeto.index(letra) + 3])
# IndexError: string index out of range

Because your string does not have a position larger than 'z'. Then you could make a module using the size of your alphabet.

for letra in 'zzz':
    print(alfabeto[(alfabeto.index(letra) + 3)% len(alfabeto)])
#'c'
#'c'
#'c'

What would make you always run the whole alphabet adding up three more and starting with the same case it bursts the size of your list.

''.join([alfabeto[(alfabeto.index(letra) + 3)% len(alfabeto)] for letra in 'stackzz'])
# 'vwdfncc'

5

You have to convert to number to make the account (with ord()) and then convert to character again (with chr()), thus:

def DeslocaASCII(texto):
    novoTexto = ''
    for letra in texto:
        numero = ord(letra)
        novoTexto += chr(numero + (-23 if numero > 87 else 3))
    return novoTexto
    
print(DeslocaASCII('ABCDEFGHIJKLMNOPQRSTUVWXYZ'))

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I am considering that only a text with uppercase characters will be sent. Validation of input text should be done elsewhere to maintain sole responsibility.

I have not tried to solve anything beyond what is in the question not to speculate where it will be used.

  • Looking from this perspective, depending on the characters they may not be printable using the ASCII table as a basis for example.

  • Yeah, I edited it to make it better.

1

def rot(s):
   return ''.join([chr(ord('A')+(ord(c)-ord('A')+3)%26) for c in s])
  • given a letter c,p=ord(c)-ord('A') , gives its position inside the letters. p {0.. 25};
  • (p+3)%26 sum 3 circularly within the segment [0..25];
  • chr(p + ord('A')) gives the letter corresponding to the position p;
  • ord('A')+(ord(c)-ord('A')+3)%26 sum 3 circularly within the quarter ['A'.. 'Z']
  • [ soma3(c) for c in s] calculates the list of worked letters present in s
  • ''.join(lista) rebuilt the string already round.

Lovers of unreadable answers may also replace Ord('A') getting:

def r(s): return ''.join([chr(65+(ord(c)-10)%26) for c in s])
  • @Isac, thank you for the explanatory suggestion; What is this "signal"? By whom? For those who see?

  • Flag is to indicate the post for analysis of others. There are several rows of analysis one for each type. Anyone can flag and anyone can analyze. Some queues require reputation to analyze, such as the low-quality queue that requires 2000 points. It is never possible to know who signaled. I think the phrase that best sums up the principle is the one that appears initially on the page of the analysis queues: "Stack Overflow in Portuguese is moderated by you."

  • @Thanks. Reminds me of the ASAE ☺...

Browser other questions tagged

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