Encrypt this---codewar刷题总结

本文总结了作者在Codewars上完成'Encrypt this!'挑战的经验。通过错误与修正,作者理解到输入数据应被视为字符串而非列表,并分享了几种聪明的解决方案,包括使用ASCII编码、字符串切片和下标操作来加密单词。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Thanks a lot for Codewar

Description:

Acknowledgments:

I thank yvonne-liu for the idea and for the example tests :)

Description:

Encrypt this!

You want to create secret messages which can be deciphered by the Decipher this!https://www.codewars.com/kata/decipher-this kata. Here are the conditions:

  1. Your message is a string containing space separated words.
  2. You need to encrypt each word in the message using the following rules:
    • The first letter must be converted to its ASCII code.
    • The second letter must be switched with the last letter
  3. Keepin' it simple: There are no special characters in the input.

Examples:

encrypt_this("Hello") == "72olle"
encrypt_this("good") == "103doo"
encrypt_this("hello world") == "104olle 119drlo"

 先来看一下我的解决方案:(太菜了)

def encrypt_this(text):
#     print(text,type(text))
    if not text:
        return ''
    temp_list=list(text.split(' '))
#     temp_list=text.splist(' ')
#     print(temp_list,type(temp_list))

    for i in temp_list:
        if len(i)==1:
            let_index=temp_list.index(i)
            temp_list.insert(let_index,str(ord(i)))
            temp_list.pop(let_index+1)
        elif len(i)==2:
            let_index=temp_list.index(i)
            temp_list.insert(let_index,str(ord(i[0]))+i[1])
            temp_list.pop(let_index+1)
        elif len(i)==3:
            let_index=temp_list.index(i)
            temp_list.insert(let_index,str(str(ord(i[0]))+i[-1]+i[1]))
            temp_list.pop(let_index+1)
        elif len(i)>=4:
            let_index=temp_list.index(i)
            temp_list.insert(let_index,str(ord(i[0])) +i[-1] +i[2:-1]+i[1])
            temp_list.pop(let_index+1)             

    return ' '.join(temp_list)

 刷这一题过程中犯了很多错误,首我没有测试输入进来的数据类型,就单纯的认为是列表,导致一开始用处理列表的方法处理数据,用splist来切割,导致出错,结果我在split外面加了个list方法,导致数据在列表之中,然后又创建了一个空列表,将处理好的字符串扔进去,用join拼接,这导致多个测试例子全都在这个列表之中,最后连成一个长字符串。处理这个问题花了很长时间,导致我认为测试代码有问题。

这时候我用的是字符串的拼接法,根本涉及不到下标。这个法子处理此问题反而简单。

但思路有问题,测试用例混杂一直解决不了。

最后所有的代码全都推倒重来,用index定位指定元素,将字符串取出拼接,insert(下标插入一个元素,这个元素占用那个下标)插入到指定位置,再用pop弹出指定位置元素。

解决之后看了一些clever的答案,真的非常惊艳,摘录几个:

def swapper(w):
    return w if len(w)<2 else w[-1] + w[1:-1] + w[0]

def encrypt_this(s):
    return ' '.join(w if not w else str(ord(w[0])) + swapper(w[1:]) for w in s.split())

这个思路是先将输入进来的数据进行切割,[1::]这些数据全部传入swapper函数之中,然后进行调换拼接,再将这个结果传回encrpyt_this中,将[0]元素转换成ascll码与传回的字符串拼接。

看来如果len=2,swapper函数中else语句后的代码块也不报错,也不会重复拼接字符串。

解法二:

list2="Thank you Piotr f al your help"
def encrypt_this(text):
    result = []
    for word in text.split():
        print(word)
        word = list(word)
# 将字符串用(split默认是空格)空格分割,遍历,取出每个单独的单词,
# list方法将每个单词放入单独的列表中,且此方法将每个单词拆分成字母
        # replace first letter with ascii code
        word[0] = str(ord(word[0]))#此时单词已成了单个的字母,下标为零的元素转为ascll码
        # switch 2nd and last letters
        if len(word) > 2:#对列表中的元素按规则进行交换
            word[1], word[-1] = word[-1], word[1]
        # add to results  再将每个列表中的字母元素重新拼接成单词
        result.append(''.join(word))
    return ' '.join(result)#将单词拼接成句子
print(encrypt_this(list2))

以上代码中的中文注释是我自己写的,这个代码虽长,但思路也很巧妙。

解法三:

def encrypt_this(text):
    if text!="":
        text=text.split(" ")
        final=[]
        for t in text:
            t1=str(ord(t[0]))
            t2=t[-1] if len(t)>1 else ""
            t3=t[2:-1] if len(t)>2 else ""
            t4=t[1] if len(t)>2 else ""
            final.append(t1+t2+t3+t4)
        return ' '.join(final)    
    else:
        return ""

 这个解决方案很不错,巧妙的利用了>号做为判断条件,任意长度的字符串都可处理,如果为空,直接返回一个空就行,

解法四:

def encrypt_this(text):
    return " ".join( [
              str(ord(p[0])) + p[2:][-1:] + p[2:len(p)-1] + p[1:2]
              for p in text.split()
           ])

这个p[2:][-1:]语句,我还不懂

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值