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:
- Your message is a string containing space separated words.
- 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
- 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:]语句,我还不懂