I had created a function where I can increment and decrement character according to certain conditions and it's working absolutely fine for incrementing before "z" and decrementing characters that exist after "a" Here is my working code for that :
str="a b c d e"
Posnew=""
Negnew=""
for i in range(len(str)):
Posnew+=chr(ord(str[i])-i)
Negnew+=chr(ord(str[i])+i)
print(Posnew)
print(Negnew)
but I don't know what logic should I create to cyclically rotate it which means if I decrement "a" then it should give "z" and if I increment "z" then it should give "a".
Desired output would be something like if i decrement "a" by 2 then it should return "y" or if I increment "z" by 5 then it should return "e"
Thanks in Advance
The following script increments and decrements each character:
import sys
str="a b c e z" if len(sys.argv) == 1 else ''.join(sys.argv[1:])
Posnew=""
Negnew=""
for i in range(len(str)):
ordstrP = ordstrN = ord(str[i])
ordstri = ordstrP - 97 # 97 == ord('a')
if ordstri in range(26): # 26 == ord('z')-ord('a')+1
ordstrP = ((ordstri + 1) % 26) + 97
ordstrN = ((ordstri + 25) % 26) + 97
Posnew+=chr(ordstrP)
Negnew+=chr(ordstrN)
print('orig:', str)
print('or+1:', Posnew)
print('or-1:', Negnew)
print('-')
Posnew=""
Negnew=""
for i in range(len(str)):
ordstrP = ordstrN = ord(str[i])
ordstri = ordstrP - 97 # 97 == ord('a')
if ordstri in range(26): # 26 == ord('z')-ord('a')+1
ordstrP = ((ordstri + (i % 26)) % 26) + 97
ordstrN = ((ordstri + 26 - (i % 26)) % 26) + 97
Posnew+=chr(ordstrP)
Negnew+=chr(ordstrN)
print('orig:', str)
print('or+i:', Posnew)
print('or-i:', Negnew)
Output : \\SO\\64788535.py
orig: a b c e z
or+1: b c d f a
or-1: z a b d y
-
orig: a b c e z
or+i: a d g k h
or-i: a z y y r
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.