题解 | #字符串加密#

字符串加密

https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3

import sys
def pdic(pkey):
    st1 = list(set(pkey))     #对传入的密钥pkey去重
    letters = [chr(x) for x in range(97,123,1)]   ##小写字母表
    st2 = set(letters) - set(st1)   #集合取差集,为剩余字符集
    lst = sorted(list(st1),key=pkey.index) + sorted(list(st2))
	#此处重要!st1是密钥列表,要按照传入pkey的字符顺序排序。st2按照正常排序即可
    dic = {}
    for i in range(26):  #构建对应密码表
        dic[list(letters)[i]] = lst[i] 
    return dic
pkey = input()
dic = pdic(pkey)
st = input()
res = [dic.get(x) for x in list(st)]  #逐字符加密
print(*(res),sep='')

全部评论

相关推荐

码农索隆:我寻思你这么激动,拿到offer了呢
点赞 评论 收藏
分享
nbdy:字太多了,写简历不是写自传,亮点难点技能点列出来就行,要简明扼要
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务