Discuss / Python / 交作业

交作业

Topic source

sy107@CHINA

#1 Created at ... [Delete] [Delete and Lock User]
def is_palindrome(n):
    y = str(n)
    L = len(y)
    for i in range(L):
        y1 = int(y[-(1 + i)])
        y2 = int(y[i])
        if y1 == y2:
            return n
        else:
            return None
# 测试:
output =list(filter(is_palindrome, range(1, 200)))
print('1~200:', output)
if output == [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('测试失败!')

sy107@CHINA

#2 Created at ... [Delete] [Delete and Lock User]
#这个才是正确答案
def is_palindrome(n):
    return str(n)==str(n)[::-1]

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

  • 1

Reply