filter

Practicing
numbers means reading the same numbers from left to right as from right to left, such as 12321, 909. Please use filter() to filter out the number of times:

# -*- coding: utf-8 -*-

def is_palindrome(n):
    return str(n)==str(n)[::-1] #Use slices to reverse strings, for example, reverse s = "abcdef" to "fedcba"

# test:
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('Test succeeded!')
else:
    print('Test failed!')

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325065619&siteId=291194637