题解 | #字符串加解密#

字符串加解密

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

def encrypt(s):
    r = []
    for i in s:
        if i == 'z':
            r.append('A')
        elif i == 'Z':
            r.append('a')
        elif i == '9':
            r.append('0')
        elif i.islower():
            ii = chr(ord(i) + 1).upper()
            r.append(ii)
        elif i.isupper():
            ii = chr(ord(i) + 1).lower()
            r.append(ii)
        else:
            r.append(str(int(i) + 1))
    return "".join(r)

def decrypt(s):
    r = []
    for i in s:
        if i == 'a':
            r.append('Z')
        elif i == 'A':
            r.append('z')
        elif i == '0':
            r.append('9')
        elif i.islower():
            ii = chr(ord(i) - 1).upper()
            r.append(ii)
        elif i.isupper():
            ii = chr(ord(i) - 1).lower()
            r.append(ii)
        else:
            r.append(str(int(i) - 1))
    return "".join(r)

print(encrypt(input()))
print(decrypt(input()))

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务