题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
# 1.提取2个入参,密匙和明文 str1=input() str2=input() # 2.把密匙字符逐个转为小写并保存到list1中 list1=[] for i in str1: if i not in list1: list1.append(i.lower()) # 3.从a~z循环。如果字符不在密匙,就补充到list1,作为value。同时把a~z放到list2,作为key i=ord('a') list2=[] while i<=ord('z'): if chr(i) not in list1: list1.append(chr(i)) if chr(i) not in list2: list2.append(chr(i)) i=i+1 # 4.从第3步的key和value,组合得到字典 dict1={} for i in range(len(list2)): dict1[list2[i]]=list1[i] # 5.遍历明文,放到list3中 list3=[] for i in str2: list3.append(i) # 6.遍历list3,根据字典key映射得到value。value组合的字符串就是加密字符串 # PS:主要区分大小写。如果遇到大写,先转换为小写映射到value,再转为为大写 str3='' for i in list3: if i>='a' and i<='z': if i in dict1: str3=str3+dict1[i] elif i>='A' and i<='Z': i.lower() if i in dict1: str3=str3+dict1[i].upper() print(str3)