题解 | #字符串加解密#

字符串加解密

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

全部评论

相关推荐

预计下个星期就能开奖吧,哪位老哥来给个准信
华孝子爱信等:对接人上周说的是这周
点赞 评论 收藏
分享
我是小红是我:学校换成中南
点赞 评论 收藏
分享
10-24 11:10
山西大学 Java
若梦难了:哥们,面试挂是很正常的。我大中厂终面挂,加起来快10次了,继续努力吧。
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务