【每日一题1101】魔法加密

:mage:t2:哈利波特最近研究出了新的魔法,他很想记在日记本上,但是又担心泄露。请编写一个函数,按照下列的规则,帮他将给定的文本进行加密操作吧:(1)首字母转换成ASCII编号(2)第二个字母与最后一个字母互换位置。

示例:
输入:" Hello",输出:" 72olle"
输入:“good”,输出:“103doo”

题目难度:简单
题目来源:codewars:Encrypt this!

def encrypt_this(text: str) -> str:
    pass

assert encrypt_this("Hello") == "72olle"
assert encrypt_this("A wise old owl lived in an oak") == "65 119esi 111dl 111lw 108dvei 105n 97n 111ka"
assert encrypt_this("The more he saw the less he spoke") == "84eh 109ero 104e 115wa 116eh 108sse 104e 115eokp"
def encrypt_this(text: str) -> str:
    word_list = []
    for word in text.split(' '):
        text_list = list(word)
        text_list[0] = str(ord(text_list[0]))
        if len(text_list) > 2:
            text_list[1], text_list[-1] = text_list[-1], text_list[1]
        word_list.append(''.join(text_list))
    return ' '.join(word_list)


assert encrypt_this("Hello") == "72olle"
assert encrypt_this("A wise old owl lived in an oak") == "65 119esi 111dl 111lw 108dvei 105n 97n 111ka"
assert encrypt_this("The more he saw the less he spoke") == "84eh 109ero 104e 115wa 116eh 108sse 104e 115eokp"
def encrypt_this(text: str) -> str:
    text_list = text.split(" ")
    result = []
    for words in text_list:
        start = str(ord(words[0]))
        if len(words) > 2:
            end = words[-1] + words[2:-1] + words[1]
        elif len(words) == 2:
            end = words[-1]
        else:
            end = ""
        result.append(start + end)
    return " ".join(result)
 public String encryptThis(String str) {
        String result = "";
        String[] split = str.split(" ");
        for (int j = 0; j < split.length; j++) {
            char[] chars = split[j].toCharArray();
            String res = "";
            for (int i = 0; i < chars.length; i++) {
                if (i == 0) {
                    res += Integer.valueOf(chars[i]);
                } else if (i == 1) {
                    res += String.valueOf(chars[i + 1]);

                } else if (i == 2) {
                    res += String.valueOf(chars[i - 1]);
                } else {
                    res += String.valueOf(chars[i]);
                }
            }
            result += res + " ";
        }
        return result.trim();
    }

转换成列表操作

def encrypt_this(text:str):
    li=text.split()
    for i in range(len(li)):
        li2=list(li[i])
        if len(li2)>=3:
            tmp=li2[1]
            li2[1]=li2[-1]
            li2[-1]=tmp
        li2[0]=str(ord(li2[0]))
        li[i]=''.join(li2)
    return ' '.join(li)
def encrypt_this(text: str) -> str:
    text=text.split(' ')
    result=[]
    for ele in text:
        ele=list(ele)
        ele[0] = str(ord(ele[0]))
        try:
            ele[1],ele[-1]=ele[-1],ele[1]
        except:
            KeyError
        result.append(''.join(ele))
    return ' '.join(result)
def encrypt_this(text: str) -> str:
    list_result=[]
    for i in text.split(' '):
        list1=list(i)
        list1[0]=str(ord(list1[0]))
        if len(list1)>2:
            list1[1],list1[-1]=list1[-1],list1[1]
        list_i=''.join(list1)
        list_result.append(list_i)
    return ' '.join(list_result)

assert encrypt_this("Hello") == "72olle"
assert encrypt_this("A wise old owl lived in an oak") == "65 119esi 111dl 111lw 108dvei 105n 97n 111ka"
assert encrypt_this("The more he saw the less he spoke") == "84eh 109ero 104e 115wa 116eh 108sse 104e 115eokp"
def encrypt_this(text: str) -> str:
    result = []
    for item in text.split(" "):
        if len(item)<2:
            result.append(str(ord(item)))
        elif len(item)<3:
            result.append(str(ord(item[0])) + item[-1])
        else:
            result.append(str(ord(item[0])) + item[-1] + item[2:-1]+item[1])

    return ' '.join(result)

assert encrypt_this("Hello") == "72olle"
assert encrypt_this("A wise old owl lived in an oak") == "65 119esi 111dl 111lw 108dvei 105n 97n 111ka"
assert encrypt_this("The more he saw the less he spoke") == "84eh 109ero 104e 115wa 116eh 108sse 104e 115eokp"
def encrypt_this(text: str) -> str:
    data = text.split(" ")
    data2 = []
    for t in data:
        if len(t) > 2:
            a = t[1]
            b = t[-1]
            t1 = t[0] + b + t[2:-1] + a
        else:
            t1 = t
        t2 = str(ord(t1[0])) + t1[1:]
        data2.append(t2)
    return ' '.join(data2)


assert encrypt_this("Hello") == "72olle"
assert encrypt_this("A wise old owl lived in an oak") == "65 119esi 111dl 111lw 108dvei 105n 97n 111ka"
assert encrypt_this("The more he saw the less he spoke") == "84eh 109ero 104e 115wa 116eh 108sse 104e 115eokp"