题解 | #字符串加解密#

字符串加解密

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


def encrypt(s1):
    s1_encrypt=''    #加密结果
    for i in s1:
        if i.isupper():
            if ord(i)==90:
                s1_encrypt += chr(ord('a'))
            else:
                s1_encrypt += chr(1+ord(i.lower()))
        elif i.islower():
            if ord(i)==122:
                s1_encrypt += chr(ord('A'))
            else:
                s1_encrypt += chr(1+ord(i.upper()))
        elif i.isdigit():
            if ord(i)==57:
                s1_encrypt += chr(ord('0'))
            else:
                s1_encrypt += chr(1+ord(i))
        else:
            s1_encrypt += i
            
    print(s1_encrypt)

def decrypt(s2):
    s2_decrypt=''  #解密结果
    for i in s2:
        if i.isupper():
            if ord(i)==65:
                s2_decrypt += chr(ord('z'))
            else:
                s2_decrypt += chr(ord(i.lower())-1)
        elif i.islower():
            if ord(i)==97:
                s2_decrypt += chr(ord('Z'))
            else:
                s2_decrypt += chr(ord(i.upper())-1)
        elif i.isdigit():
            if ord(i)==48:
                s2_decrypt += chr(ord('9'))
            else:
                s2_decrypt += chr(ord(i)-1)
        else:
            s2_decrypt += i
            
    print(s2_decrypt)

while 1:
    try:
        s1=input()
        s2=input()
        encrypt(s1)
        decrypt(s2)
    except:
        break







全部评论

相关推荐

牛客101244697号:这个衣服和发型不去投偶像练习生?
点赞 评论 收藏
分享
10-17 16:07
门头沟学院 Java
牛牛大你18号:在汇报,突然弹出来,,领导以为我在准备跳槽,刚从领导办公室谈心出来
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务