Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的短Rot(N)解码函数_Python_Decode_Encode - Fatal编程技术网

python中的短Rot(N)解码函数

python中的短Rot(N)解码函数,python,decode,encode,Python,Decode,Encode,我对python有点陌生,我想知道如何将这个rot(n)编码函数 def rot_encode(n): from string import ascii_lowercase as lc, ascii_uppercase as uc lookup = str.maketrans(lc + uc, lc[n:] + lc[:n] + uc[n:] + uc[:n]) return lambda s: s.translate(lookup) print(rot_alpha(1

我对python有点陌生,我想知道如何将这个rot(n)编码函数

def rot_encode(n):
    from string import ascii_lowercase as lc, ascii_uppercase as uc
    lookup = str.maketrans(lc + uc, lc[n:] + lc[:n] + uc[n:] + uc[:n])
    return lambda s: s.translate(lookup)

print(rot_alpha(13)('Hello World'))
到解码函数

def rot_encode(n):
    from string import ascii_lowercase as lc, ascii_uppercase as uc
    lookup = str.maketrans(lc + uc, lc[n:] + lc[:n] + uc[n:] + uc[:n])
    return lambda s: s.translate(lookup)

print(rot_alpha(13)('Hello World'))
我不想使用python的内置功能来编码或解码,我想重新创建它


提前感谢

您无需重新创建任何内容。只需将字母移到另一个方向,即调用
rot_encode(-13)
来解码先前编码的字符串,而不是
rot_encode(13)

x = rot_encode(13)('Hello World')
y = rot_encode(-13)(x)
print(x) # Uryyb Jbeyq
print(y) # Hello World
当然,如果您愿意,也可以将其包装到
rot_decode
函数中

def rot_decode(n):
    return rot_encode(-n)

你知道
maketrans
translate
在这里是如何工作的吗?不知道,我是在网上找到的,正在考虑如何转换它。(仍然是noob:p)可能的重复我想自己重新创建它,而不是使用内置函数:/address:当然,在ROT13的特殊情况下,甚至不需要这样做,因为13正好是26的一半(字母表中的字母),因此编码两次也会解码字符串,即
ROT13(ROT13(x))==x