题解 | #字符串加解密#

字符串加解密

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

import string

upper_char_list = list(string.ascii_uppercase)
lower_char_list = list(string.ascii_lowercase)

while True:
    try:
        want_str = input()
        new_str_list = []
        for each in want_str:
            if each.isalpha():
                if each == "z":
                    new_char = "A"
                    new_str_list.append(new_char)
                    continue
                if each == "Z":
                    new_char = "a"
                    new_str_list.append(new_char)
                    continue
                if each.isupper():
                    want_index = upper_char_list.index(each)
                    new_char = upper_char_list[want_index + 1]
                    new_str_list.append(new_char.lower())
                else:
                    want_index = lower_char_list.index(each)
                    new_char = lower_char_list[want_index + 1]
                    new_str_list.append(new_char.upper())
            else:
                if each == "9":
                    new_char = "0"
                    new_str_list.append(new_char)
                    continue
                new_char = str(int(each) + 1)
                new_str_list.append(new_char)
        
        res = "".join(new_str_list)
        print(res)
        
        want_str = input()
        new_str_list = []
        for each in want_str:
            if each.isalpha():
                if each == "a":
                    new_char = "Z"
                    new_str_list.append(new_char)
                    continue
                if each == "A":
                    new_char = "z"
                    new_str_list.append(new_char)
                    continue
                if each.isupper():
                    want_index = upper_char_list.index(each)
                    new_char = upper_char_list[want_index - 1]
                    new_str_list.append(new_char.lower())
                else:
                    want_index = lower_char_list.index(each)
                    new_char = lower_char_list[want_index - 1]
                    new_str_list.append(new_char.upper())
            else:
                if each == "0":
                    new_char = "9"
                    new_str_list.append(new_char)
                    continue
                new_char = str(int(each) - 1)
                new_str_list.append(new_char)
        
        res = "".join(new_str_list)
        print(res)
    except EOFError:
        break
相当繁琐。。。
全部评论

相关推荐

Natrium_:这时间我以为飞机票
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务