玩python总有种自己在耍杂技的快感
Topic source我花了六个小时写的,对比一下。好像不能插入代码了呢。
-- coding: utf-8 --
from functools import reduce
def is_palindrome(n): if n < 10: return True else:
L = []
L2 = list(map(lambda x: x, str(n)))
while L2 != []:
L.append(L2[-1])
L2.pop()#将拆分的数字进行倒序排列
S = list(map(int, L))
def fn(x,y):
return x * 10 + y
return reduce(fn, S) == n
测试:
output = filter(is_palindrome, range(1, 1000)) print(list(output))
- 1
小肠杆菌
一行搞定…… print(list(filter((lambda n:str(n)==str(n)[::-1]), range(1, 10000))))