Discuss / Python / Python 的魅力太大了,每次我用了好长代码写出来的答案。你们都是一句代码的事情。郁闷!!!!!!!!

Python 的魅力太大了,每次我用了好长代码写出来的答案。你们都是一句代码的事情。郁闷!!!!!!!!

Topic source

郑男6

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

My answer:

-- coding: utf-8 --

def is_palindrome(n): ch=str(n) t=len(ch) y=0 for x in ch: if ch[y]!=ch[t-1-y]: return False y=y+1 return True

测试:

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('测试失败!')

大神的答案:
def is_palindrome(n):
return str(n) == str(n)[::-1]

  • 1

Reply