题解 | #字符串加密#
字符串加密
http://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
首先去重输入的字符串,然后转换为对应的密码表 然后遍历需要转换的字符,找到字符在非密文字符串中索引,根据索引找到对应的密码表上的值并替换,最后打印
import string
def distinct_words(words: str):
return sorted(set(words), key=words.index)
def password_letters(words: list):
for x in string.ascii_lowercase:
if x not in words:
words.append(x)
return words
if __name__ == '__main__':
while True:
try:
lower_case = string.ascii_lowercase
pwd_list = password_letters(distinct_words(input()))
source = [letter for letter in input()]
for idx in range(len(source)):
tmp = source[idx]
if tmp in lower_case:
source[idx] = pwd_list[lower_case.index(tmp)]
else:
source[idx] = pwd_list[lower_case.index(tmp)].upper()
for x in source:
print(x, end='')
print()
except EOFError:
break