题解 | #字符串加解密#

字符串加解密

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

import sys
import string

string_lower_list = list(string.ascii_lowercase)
string_upper_list = list(string.ascii_uppercase)
nums_list = [str(each) for each in range(0, 10)]


def encry_str(word):
    res = []
    for each in word:
        if each in string_lower_list:
            container = string_lower_list
        elif each in string_upper_list:
            container = string_upper_list
        else:
            container = nums_list

        if container.index(each) == len(container) - 1:
            want = container[0]
        else:
            want = container[container.index(each) + 1]

        if want in string_lower_list:
            res.append(want.upper())
        elif want in string_upper_list:
            res.append(want.lower())
        else:
            res.append(want)
    return "".join(res)


def deencry_str(word):
    res = []
    for each in word:
        if each in string_lower_list:
            container = string_lower_list
        elif each in string_upper_list:
            container = string_upper_list
        else:
            container = nums_list

        if container.index(each) == 0:
            want = container[-1]
        else:
            want = container[container.index(each) - 1]

        if want in string_lower_list:
            res.append(want.upper())
        elif want in string_upper_list:
            res.append(want.lower())
        else:
            res.append(want)
    return "".join(res)


count = 0
for line in sys.stdin:
    if count == 0:
        encry = line.split()[0]
        res = encry_str(encry)
        print(res)
    if count == 1:
        deencry = line.split()[0]
        res = deencry_str(deencry)
        print(res)

    count += 1

全部评论

相关推荐

11-27 12:36
已编辑
门头沟学院 前端工程师
Apries:这个阶段来说,很厉害很厉害了,不过写的简历确实不是很行,优势删掉吧,其他的还行
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务