题解 | #字符串加密#
字符串加密
http://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
def fun(key, s):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
key = key.lower()
newkey = ''
newalphabet = alphabet
#得到密钥
for i, k in enumerate(key):
if i == key.index(k):#实现了去重操作!如key.index('l') = 4 (只返回第一次出现时的位置!)
newkey += k
newalphabet = newalphabet.replace(k, '')
newkey = newkey + newalphabet
#得到密文
d = dict(zip(alphabet, newkey))#alphabet是d.keys() newkey是d.values()
#字母字符的大小写状态保留 空格也要保留
temp = []
for i in s:
if i == ' ':
temp.append(' ')
elif i.islower():
temp.append(d[i])
else:
i = i.lower()
temp.append(d[i].upper())
res = ''.join(temp)
return res
while True:
try:
key = input()
s = input()
print(fun(key, s))
except:
break
'''
TRAILBLAZERS
Attack AT DAWN
'''
360集团公司氛围 407人发布