Codewars练习

记录一下比较聪明的codewars练习题解决方案,不得转载。

2017/12/19

You will be given a string and you task is to check if it is possible to convert that string into a palindrome by removing a single character. If the string is already a palindrome, return "OK". If it is not, and we can convert it to a palindrome by removing one character, then return "remove one", otherwise return "not possible". The order of the characters should not be changed.

best practice

1 def solve(s):
2     isOK = lambda x: x == x[::-1]
3     
4     return ("OK" if isOK(s)  else
5             "remove one" if any( isOK(s[:i]+s[i+1:]) for i in range(len(s)) ) else
6             "not possible")

主要是s[:i]+s[i+1:]提取字符串,any()思路很好啊,我写的就复杂很多了。

 

posted @ 2017-12-19 09:47  orange1002  阅读(310)  评论(0编辑  收藏  举报