I would like to walk 3 positions with a letter from a string , for example, make the letter A turn D, I tried the second command:
texto[c] = texto[c] + 3
But it still does not work, what would be the right way to do it?
I would like to walk 3 positions with a letter from a string , for example, make the letter A turn D, I tried the second command:
texto[c] = texto[c] + 3
But it still does not work, what would be the right way to do it?
If I understand you, you want to do something like Cesar's cipher. Given a string, you need to walk with each character plus 3 positions.
You can use a basic 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 base of strings. So let's get a string to put in the game:
string = 'stack'
The string has a method called .index
that 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 through 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 whole string rotated. However, there are some problems, such 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 greater than 'z'. So 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 you always run the whole alphabet by adding three more and starting with the same case it pops the size of your list.
''.join([alfabeto[(alfabeto.index(letra) + 3)% len(alfabeto)] for letra in 'stackzz'])
# 'vwdfncc'
You have to convert to number to make the account (with ord()
) and then convert to character again (with chr()
), like this:
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'))
See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .
I'm considering that only text with upper-case 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 so as not to speculate on where it will be used.
def rot(s):
return ''.join([chr(ord('A')+(ord(c)-ord('A')+3)%26) for c in s])
c
, p=ord(c)-ord('A')
, gives its position within the letters. p ∈ {0..25}; (p+3)%26
sum 3 circularly within 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 circulamente within the section ['A' .. 'Z'] [ soma3(c) for c in s]
computes the list of rounded letters present in s
''.join(lista)
rebuilds the already-rotated string. Lovers of unreadable answers can still replace ord ('A') by getting:
def r(s): return ''.join([chr(65+(ord(c)-10)%26) for c in s])