题解 | #HJ29 字符串加解密#

字符串加解密

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

Python版本

Python 里无法直接对单个字符作差得到ASCII码(Unicode码),所以需要借助 ordchr,或者你能记住 aA0 的ASCII码也行……

def crypto(string):
    temp = []
    for c in string:
        if ord('a') <= ord(c) <= ord('z'):
            temp.append(chr((ord(c.upper())-ord('A')+1)%26 + ord('A')))
        elif ord('A') <= ord(c) <= ord('Z'):
            temp.append(chr((ord(c.lower())-ord('a')+1)%26 + ord('a')))
        elif ord('0') <= ord(c) <= ord('9'):
            temp.append(str((int(c)-0+1)%10+0))
        else:
            temp.append(c)
    return ''.join(temp)

def decrypt(string):
    temp = []
    for c in string:
        if ord('a') <= ord(c) <= ord('z'):
            temp.append(chr((ord(c.upper())-ord('A')+25)%26 + ord('A')))
        elif ord('A') <= ord(c) <= ord('Z'):
            temp.append(chr((ord(c.lower())-ord('a')+25)%26 + ord('a')))
        elif ord('0') <= ord(c) <= ord('9'):
            temp.append(str((int(c)-0+9)%10+0))
        else:
            temp.append(c)
    return ''.join(temp)

while True:
    try:
        string_raw = input()
        string_cpt = input()
        print(crypto(string_raw))
        print(decrypt(string_cpt))
    except Exception:
        break

全部评论

相关推荐

这是什么操作什么意思,这公司我服了...
斯派克spark:意思是有比你更便宜的牛马了
点赞 评论 收藏
分享
Southyeung:我说一下我的看法(有冒犯实属抱歉):(1)简历不太美观,给我一种看都不想看的感觉,感觉字体还是排版问题;(2)numpy就一个基础包,机器学习算法是什么鬼?我感觉你把svm那些写上去都要好一点。(2)课程不要写,没人看,换成获奖经历;(3)项目太少了,至少2-3个,是在不行把网上学习的也写上去。
点赞 评论 收藏
分享
评论
1
5
分享

创作者周榜

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