華為程式設計題-加密解密

Allen-Liu發表於2017-09-15

問題描述:



本地執行無任何問題,牛客上只讀取一行輸出,不知道為什麼。方法也不是什麼高效的方法,期待牛人分享更好的:

__time__ = "2017/9/15"
'''This program used to '''
#print('a' + 1)
def jiami(in1):
    out1 = []
    for i in in1:
        out1.append(i)
    for i in range(len(out1)):
        if (out1[i].isalpha()):
            tmp = out1[i].swapcase()
            if ord(tmp) >= 65 and ord(tmp)<= 90 :
                if (tmp == 90):
                    out1[i] = 'A'
                else:
                    out1[i] = chr(ord(tmp) + 1)
            elif (ord(tmp) >= 97 and ord(tmp) <= 122):
                if (ord(tmp) == 122):
                    out1[i] = 'a'
                else:
                    out1[i] = chr(ord(tmp) + 1)
        else:
            if (int(out1[i]) + 1 == 10):
                out1[i] = '0'
            else:
                out1[i] = str(int(out1[i]) + 1)
    out = ''.join(out1)
    return out

def jiemi(in2):
    out2 = []
    for i in in2:
        out2.append(i)
    for i in range(len(out2)):
        if (out2[i].isalpha()):
            tmp = out2[i].swapcase()
            if (ord(tmp) >= 65 and ord(tmp) <= 90 ):
                if (ord(tmp) == 65):
                    out2[i] = 'Z'
                else:
                    out2[i] = chr(ord(tmp) - 1)
            elif (ord(tmp) >= 97 and ord(tmp) <= 122):
                if (ord(tmp) == 97):
                    out2[i] = 'z'
                else:
                    out2[i] = chr(ord(tmp) - 1)
        else:
            if (int(out2[i])  == 0):
                out2[i] = '9'
            else:
                out2[i] = str(int(out2[i]) - 1)
    out = ''.join(out2)
    return out

in1 = input()
in2 = input()
print(jiami(in1))
print(jiemi(in2))


相關文章