Discuss / Python / filter、递归、切片完成作业

### filter、递归、切片完成作业

Topic source

#### 用户7642543399

#1 Created at ... [Delete] [Delete and Lock User]

def is_palindrome(n):

if n < 10 :

return True

nn = str(n)

if nn[:1] == nn[-1:] :

if len(nn) == 2:

return True

else :

return is_palindrome(int(nn[1:len(nn)-1]))

else:

return False

#### 用户7642543399

#2 Created at ... [Delete] [Delete and Lock User]

def is_palindrome(n):

nn = str(n)

if nn[:1] == nn[-1:] :

if len(nn) < 3:

return True

else :

return is_palindrome(int(nn[1:len(nn)-1]))

else:

return False

#### 用户7642543399

#3 Created at ... [Delete] [Delete and Lock User]

5行代码版：

if str(n)[:1] == str(n)[-1:] :

if len(str(n)) < 3:

return True

else :

return is_palindrome(int(str(n)[1:len(str(n))-1]))

#### starry

#4 Created at ... [Delete] [Delete and Lock User]

#### Mr.黑牛

#5 Created at ... [Delete] [Delete and Lock User]

2行版：

def is_palindrome(n):

return str(n)==str(n)[::-1]

#### 刘明阳

#6 Created at ... [Delete] [Delete and Lock User]

return is_palindrome(int(nn[1:len(nn)-1])) 这行代码是有问题的，比如输入2012这个数，则会等效判定为“212”这个数。

``````return is_palindrome(nn[1:- 1])
``````

#### 眼睛里

#7 Created at ... [Delete] [Delete and Lock User]

def is_palindrome(n):

s = str(n)

i, j = 0, len(s) - 1

while i < j and s[i] == s[j]:

i += 1

j -= 1

if i >= j:

return True

return False

#### lucky lucy

#8 Created at ... [Delete] [Delete and Lock User]

• 1