码迷,mamicode.com
首页 > 其他好文 > 详细

Codewars练习

时间:2017-12-19 12:28:36      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:not   ram   解决方案   方案   char   rac   string   har   gpo   

记录一下比较聪明的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练习

标签:not   ram   解决方案   方案   char   rac   string   har   gpo   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!