number of times
Reading from left to right and right to left is the same number, 606,999
uses filter() to find the number of times.
From this, my idea is to judge whether the numbers on the left and right are equal.
def is_palindrome(n):
L = []
for i in str(n):
L.append(i)
L1 = L[0:int((len(L)+1)/2)]
L2 = L[int((len(L)-1)/2):]
if set(L1) == set(L2):
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('测试失败!')
comment area code
def is_palindrome(n):
t = str(n) #将整型转化成字符串
a = t[::-1] #字符串切片反转
if a == t:
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('测试失败!')