题解 | #字符串加密# sorted()/zip()
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
几个重点函数: sorted(,key=key.index),dict(zip(l,tmp))
while True:
try:
key,code=input(),input()
#排序后按原来索引顺序
tmp=sorted(set(key),key=key.index)
#考虑到加密内容有空格,所以加上
l=list("abcdefghijklmnopqrstuvwxyz ")
for i in l:
if i not in tmp:
tmp.append(i)
#zip()打包 dict()转字典类型
dic=dict(zip(l,tmp))
res=""
for i in code:
res+=dic[i]
print(res)
except:
break
华为机试(python3) 文章被收录于专栏
少壮不努力,老大勤刷题
查看2道真题和解析