题解 | 字符串加解密

字符串加解密

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

s = input()
t = input()

def encrypt(s: str) -> str:
    result = ''
    for char in s:
        if char.isalpha():
            char = char.swapcase()
            if 'a' <= char < 'z' or 'A' <= char < 'Z':
                char = chr(ord(char)+1)
            elif char == 'Z':
                char = 'A'
            elif char == 'z':
                char = 'a'
            result += char
        elif char.isdigit():
            if '0' <= char < '9':
                char = str(int(char)+1)
            else:
                char = '0'
            result += char
    return result

def decode(t: str) -> str:
    result = ''
    for c in t:
        if c.isalpha():
            c = c.swapcase()
            if 'a' < c <= 'z' or 'A' < c <= 'Z':
                c = chr(ord(c)-1)
            elif c == 'A':
                c = 'Z'
            elif c == 'a':
                c = 'z'
            result += c
        elif c.isdigit():
            if '0' < c <= '9':
                c = str(int(c)-1)
            else:
                c = '9'
            result += c
    return result


print(encrypt(s))
print(decode(t))

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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