HW.字符串加密

HW.字符串加密_第1张图片

while True:
    """
    依次遍历key和字母表
    A被编码成B,A要被记录,B也要被记录,用arry记录已经被编码的A,dict记录编码的结果占用了哪些
    """
    try:
        key = input()
        info = list(input())

        array =["#"] * 26
        dict = {}
        index = 0
        for char in key:
            if char not in dict:
                array[index] = char
                dict[char] =1
                index +=1
        for i in range(26):
            char = chr(ord("a") + i)
            if char not in dict:
                array[index] = char
                index += 1
                dict[char] =1
        for i in range(len(info)):
            if info[i].islower():
                info[i] = array[ord(info[i]) - ord("a")]
            else:
                info[i] = array[ord(info[i].lower())].upper()
        print("".join(info))
    except:
        break

你可能感兴趣的:(LeetCode)