【每日一题1123】解密

:woman_mage: 已知有一个加密后的字符串,例如"r slkv mlylwb wvxlwvh gsrh nvhhztv",通过仔细研究,我们会发现它表示的是"i hope nobody decodes this message",其中的规律就是解密后的真实字母刚好是在字母表中的对称位置上的字母,例如字母 a 对应字母 z ,字母 b 对应字母 y,以此类推。

请编写一个函数,接收一个只包含小写字母的字符串,返回解密后的字符串。

示例:
输入:“sr”,输出:“hi”
输入:“svool”,输出:“hello”

题目难度:简单
题目来源:codewars:Decoding a message

def solution(secret_msg: str) -> str:
    # your code

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:
    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_msg: str) -> str:
    return ''.join([chr(abs(123-(ord(s)-97)-1)) if s != ' ' else ' ' for s in secret_msg])


assert solution("sr") == "hi"
assert solution("svool") == "hello"
assert solution("r slkv mlylwb wvxlwvh gsrh nvhhztv") == "i hope nobody decodes this message"
1 个赞
def solution(secret_msg: str) -> str:
    list2=[]
    x=''.join([chr(i) for i in range(97, 123)])
    for i in secret_msg.split(' '):
        list1 = []
        for j in i:
            I=x[(x.index(j)+1)*(-1)]
            list1.append(I)
        list2.append(''.join(list1))
    return ' '.join(list2)

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"
    public String solution2(String s){
        char[] chars = s.toCharArray();
        String res ="";
        for (char c:chars) {
            res += (char)(219-(int)c);
        }
        return res;
    }
def solution(secret_msg: str) -> str:
    res_list = []
    for s in secret_msg:
        if s != " ":
            # 97 是 a 的 ASCII 值, 122 是 z 的 ASCII 值
            str_to = chr(abs(122 - (ord(s) - 97)))
            res_list.append(str_to)
        else:
            res_list.append(s)
    return "".join(res_list)
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"