简体   繁体   中英

Char shift to left side using python

I am new to python. I want to shift char by its position. for eg

pos= 3 then replace c by z , b by y , a by x, Same like C by Z , B by Y and A by X

What I have tried ? I have tried with hardcoded values as,

for i in s:
     if ord(i) == 99:
          return z
     if ord(i) == 98:
          return x
     ...
     ...

Is there is any builtin function is available to do this ? or Any other simpler way to achieve this ?

Edit:

if string would be "abcdef" the output would be "xyzabc"

I user input "AbCdEf" then output would be "XyZaBc"

You can use ord combined with chr :

ord("e") + 3
   -> 104
chr(ord("e") + 3)
   -> 'h'

From your requirements, I understand that you need to demonstrate a Substitution Cipher . It is very simple to implement. I will give you function outline pseudocode:

char substitutionCipher(int leftRotateBy, char alphabet) {
    int pos = (int) alphabet - (int) 'a';
    pos -= leftRotateBy;
    if(pos < 0) {
       pos += 26;
    }
    return (char) (pos + (int) 'a');
}

Please note that this outline will work only for lowercase letters. You can modify it to work for uppercase letters also. Thanks!

If you're using Python 2 and you are wanting to convert entire sentences, consider making a character translation table. https://docs.python.org/2/library/string.html#string.maketrans

import string

def caesar_shift(secret, shift):
    lcase = string.ascii_lowercase
    ucase = string.ascii_uppercase
    shift = shift % len(lcase) # force shift to be between 0 and 25, inclusive
    trans = string.maketrans(
        ''.join([lcase,ucase]),
        ''.join([lcase[shift:], lcase[shift:],
                  ucase[shift:], ucase[:shift]]))
    return string.translate(secret, trans)

Keep in mind that string.maketrans and string.translate were removed in deprecated in Python 3 as part of improving Python's Unicode string handling.

Use negative or positive values to shift left/right. https://en.wikipedia.org/wiki/Caesar_cipher

import string

def convert(c, pos):
        charset = string.lowercase
        index = charset.find(c)
        if index >= 0:
                return charset[(index - pos) % len(charset)]

        charset = string.uppercase
        index = charset.find(c)
        if index >= 0:
                return charset[(index - pos) % len(charset)]

        return c

assert convert("c", 3) == "z"
assert convert("b", 3) == "y"
assert convert("a", 3) == "x"
assert convert("C", 3) == "Z"
assert convert("B", 3) == "Y"
assert convert("A", 3) == "X"

Update for those who are interested in better performance:

import string

def get_converter(pos):
        def rot(s, pos):
                offset = -pos % len(s)
                return s[offset:] + s[:offset]
        def getzip(charset, pos):
                return zip(charset, rot(charset, pos))
        def getdict(pos):
                return dict(getzip(string.ascii_lowercase, pos) + getzip(string.ascii_uppercase, pos))
        chardict = getdict(pos)
        def converter(c):
                return chardict.get(c, c)
        return converter

convert = get_converter(3)

assert convert("c") == "z"
assert convert("b") == "y"
assert convert("a") == "x"
assert convert("C") == "Z"
assert convert("B") == "Y"
assert convert("A") == "X"
assert convert("!") == "!"

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM