题解 | #字符串加解密#

字符串加解密

https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a

import re
def lock(list):
    res = ''
    for i in range(len(list)):
        if re.match(r'[a-y]',list[i]):
            res += chr(ord(list[i])+1).upper()
        elif re.match(r'[A-Y]',list[i]):
            res += chr(ord(list[i])+1).lower()
        elif list[i] == 'z':
            res += 'A'
        elif list[i] == 'Z':
            res += 'a'
        elif re.match(r'[0-9]',list[i]):
            res += str((int(list[i])+1)%10)# 对数字+1再对10取模解决9-0的加密过程
        else:
            res += list[i]
    return res

def unlock(list):
    res = ''
    for i in range(len(list)):
        if re.match(r'[b-z]', list[i]):
            res += chr(ord(list[i])-1).upper()
        elif re.match(r'[B-Z]',list[i]):
            res += chr(ord(list[i])-1).lower()
        elif list[i] == 'a':
            res += 'Z'
        elif list[i] == 'A':
            res += 'z'
        elif re.match(r'[0-9]',list[i]):
            res += str((int(list[i])+9)%10)# 对数字+9再对10取模解决0-9的解密过程
        else:
            res += str(list[i])
    return res

while True:
    try:
        print(lock(input()))
        print(unlock(input()))
    except:
        break

全部评论

相关推荐

10-29 18:20
济南大学 Java
用微笑面对困难:他不是人事吗,怎么净特么不干人事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务