[英]Char shift to left side using python
I am new to python. 我是python的新手。 I want to shift char by its position. 我想按其位置移动char。 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 pos = 3然后用z替换c,用y替换b,用x替换a,像用C替换C一样,用Y替换B并用X替换A
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"
如果字符串为"abcdef"
则输出为"xyzabc"
I user input "AbCdEf"
then output would be "XyZaBc"
我用户输入"AbCdEf"
然后输出为"XyZaBc"
You can use ord
combined with chr
: 您可以将ord
与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. 如果您使用的是Python 2,并且想要转换整个句子,请考虑制作一个字符转换表。 https://docs.python.org/2/library/string.html#string.maketrans 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. 请记住,作为改进Python的Unicode字符串处理的一部分,Python 3中已弃用了string.maketrans和string.translate。
Use negative or positive values to shift left/right. 使用负值或正值向左/向右移动。 https://en.wikipedia.org/wiki/Caesar_cipher https://zh.wikipedia.org/wiki/凯撒密码
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("!") == "!"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.