Discuss / Python / 习题解答

习题解答

Topic source

cherose_bj

#1 Created at ... [Delete] [Delete and Lock User]
def is_palindrome(n):
    if str(n) == str(n)[::-1]: return True   
    else: return False

output = filter(is_palindrome, range(1, 1000))
print(list(output))

cherose_bj

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

还可以用递归

def is_palindrome(n):
    n=str(n)
    if len(n) <= 1:
        return True
    else:
        return n[0] == n[-1] and is_palindrome(n[1:-1])

output = filter(is_palindrome, range(1, 1000))
print(list(output))

  • 1

Reply