题解 | #字符串加解密#
字符串加解密
http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
继续用string模块取处理,获取源位置和目的位置对应的元素,进行转化即可,加密就从源到目的,解密就是从目的到源。
同时使用while True取循环读取信息
from string import ascii_lowercase, ascii_uppercase, digits
source = list(ascii_lowercase + ascii_uppercase + digits)
target = list(ascii_uppercase[1:] + ascii_uppercase[0] + ascii_lowercase[1:] + ascii_lowercase[0] + digits[1:] + digits[0])
def encrypt(strings:str):
result = ''
for case in strings:
if 'A' <= case <= 'z' or '0' <= case <= '9':
result += target[source.index(case)]
else:
result += case
return result
def decrypt(strings:str):
result = ''
for case in strings:
if 'A' <= case <= 'z' or '0' <= case <= '9':
result += source[target.index(case)]
else:
result += case
return result
while True:
try:
print(encrypt(input()))
print(decrypt(input()))
except EOFError: break