Discuss / Python / 判断回数

判断回数

Topic source

里昂tcxy

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

#整数转化成字符串,利用前半段与后半段相等得到回数

def is_palindrome(n1):

    n = str(n1)

    length = len(n)

    halflen = length//2

    i = 0

    while i < halflen:

        if n[i] == n[length-i-1]:#左边的数等于右边的数,下标从0开始,要减1

            i = i+1

        else:

            return None#不是回数返回空

    return n1

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

print('1-1000中的回数有:',list(output))

里昂tcxy

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

结合python语言的特性:

def is_palindrome(n1):

    n = str(n1)

    if n[::] == n[::-1]:

        return n1

    else:

        return None

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

print('1-1000中的回数有:',list(output))


  • 1

Reply