题解 | #字符串加解密#简单模拟

字符串加解密

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

def code(string):
    res = ''
    for x in string:
        if x.isdigit():
            res += str((int(x)+1)%10)
        elif x.isupper() and x != 'Z':
            res += chr(ord(x)+1).lower()
        elif x == 'Z':
            res += 'a'
        elif x.islower() and x != 'z':
            res += chr(ord(x)+1).upper()
        elif x == 'z':
            res += 'A'    
    return res

def decode(string):
    res = ''
    for x in string:
        if x.isdigit():
            res += str((int(x)-1)) if int(x)-1 >= 0 else '9'
        elif x.isupper() and x != 'A':
            res += chr(ord(x)-1).lower()
        elif x == 'A':
            res += 'z'
        elif x.islower() and x != 'a':
            res += chr(ord(x)-1).upper()
        elif x == 'a':
            res += 'Z'
    return res

while 1:
    try:
        s1, s2 = input(), input()
        print(code(s1))
        print(decode(s2))
    
    except:
        break
全部评论

相关推荐

自由水:笑死了,敢这么面试不敢让别人说
点赞 评论 收藏
分享
野猪不是猪🐗:我assume that你must技术aspect是solid的,temperament也挺good的,however面试不太serious,generally会feel style上不够sharp
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务