已知有一个加密后的字符串,例如"r slkv mlylwb wvxlwvh gsrh nvhhztv",通过仔细研究,我们会发现它表示的是"i hope nobody decodes this message",其中的规律就是解密后的真实字母刚好是在字母表中的对称位置上的字母,例如字母 a 对应字母 z ,字母 b 对应字母 y,以此类推。
def solution(secret_msg: str) -> str:
Skeys=[chr(i) for i in range(97,123)]
Svalues = [chr(i) for i in range(122,96,-1)]
data = dict(zip(Skeys,Svalues))
data[" "] = " "
return ''.join([data[item] for item in list(secret_msg)])
assert solution("sr") == "hi"
assert solution("svool") == "hello"
assert solution("r slkv mlylwb wvxlwvh gsrh nvhhztv") == "i hope nobody decodes this message"
def solution(secret:str)->str:
alphabet = string.ascii_lowercase
result = ''
for i in range(0,len(secret)):
if secret[i] == ' ':
result = result + ' '
else:
index = -1 - alphabet.rindex(secret[i])
result = result + alphabet[index]
return result
assert solution("sr") == "hi"
assert solution("svool") == "hello"
assert solution("r slkv mlylwb wvxlwvh gsrh nvhhztv") == "i hope nobody decodes this message"
def solution(secret_msg: str) -> str:
temp = ''
for c in secret_msg:
if c == ' ':
temp += c
else:
temp += chr(ord('z') + ord('a') - ord(c))
return temp
def solution(secret_msg: str) -> str:
table = 'abcdefghijklmnopqrstuvwxyz'
result = ''
for w in secret_msg:
if w.strip() != "":
result += table[25 - table.index(w)]
else:
result += w
return result
def solution(secret_msg: str) -> str:
str_list = []
for i in range(ord('a'), ord('z') + 1):
str_list.append(chr(i))
result = ""
for i in secret_msg:
if i == " ":
result += " "
else:
result += str_list[-(str_list.index(i) + 1)]
return result
assert solution("sr") == "hi"
assert solution("svool") == "hello"
assert solution("r slkv mlylwb wvxlwvh gsrh nvhhztv") == "i hope nobody decodes this message"