首页 > 其他 > 详细

Codewars练习

时间:2017-12-19 10:20:46      阅读:214      评论:0      收藏:0      [点我收藏+]

记录一下比较聪明的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()思路很好啊,我写的就复杂很多了。

 

Codewars练习

原文:http://www.cnblogs.com/zxzhu/p/8063998.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!