编码解码器

字符串加解密

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

def encode(string):
    string = list(string)
    for i in range(len(string)):
        if string[i].isalpha():
            if string[i].islower():
                if string[i] != 'z':
                    string[i] = chr(ord(string[i].upper()) + 1)
                else:
                    string[i] = 'A'
            else:
                if string[i] != 'Z':
                    string[i] = chr(ord(string[i].lower()) + 1)
                else:
                    string[i] = 'a'
        else:
            if string[i] != '9':
                string[i] = str(int(string[i]) + 1)
            else:
                string[i] = '0'
    return ''.join(string)

def decode(string):
    string = list(string)
    for i in range(len(string)):
        if string[i].isalpha():
            if string[i].islower():
                if string[i] != 'a':
                    string[i] = chr(ord(string[i].upper()) - 1)
                else:
                    string[i] = 'Z'
            else:
                if string[i] != 'A':
                    string[i] = chr(ord(string[i].lower()) - 1)
                else:
                    string[i] = 'z'
        else:
            if string[i] != '0':
                string[i] = str(int(string[i]) - 1)
            else:
                string[i] = '9'
    return ''.join(string)
while True:
    try:
        encode_str = input()
        decode_str = input()
        print(encode(encode_str))
        print(decode(decode_str))
    except:
        break

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务