Discuss / Python / 练习题

练习题

Topic source

唯情恋昉

#1 Created at ... [Delete] [Delete and Lock User]
def is_palindrome(n):
    s = str(n)
    i = 0
    flag = True
    while i <= int(len(s)/2):
        # 由s[0] = s[-1]可以推出规律
        if s[i] != s[-(i+1)]:
            flag = False
        i += 1
    if flag:
        return n

# 测试:
output = filter(is_palindrome, range(1, 1000))
print('1~1000:', list(output))
if list(filter(is_palindrome, range(1, 200))) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191]:
    print('测试成功!')
else:
    print('测试失败!')

  • 1

Reply