Discuss / Python / 玩python总有种自己在耍杂技的快感

玩python总有种自己在耍杂技的快感

Topic source

小肠杆菌

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

一行搞定…… print(list(filter((lambda n:str(n)==str(n)[::-1]), range(1, 10000))))

小肠杆菌

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

在控制台下面还能再短点 [ n for n in range(10000) if str(n)==str(n)[::-1]]

所以倒序这么写就可以了么 [::-1],我居然写了一大堆。

我花了六个小时写的,对比一下。好像不能插入代码了呢。

-- 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))

TimorYang

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

老铁,666


  • 1

Reply